Spring controls COMMIT transaction operations for multiple tables

Source: Internet
Author: User

A. Spring configuration file is as follows:

<bean id= "test" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/>
<property name= "url" value= "Jdbc:oracle:thin:@192.168.1.192:1521:test"/>
<property name= "username" value= "test"/>
<property name= "password" value= "test"/>
<property name= "InitialSize" value= "5"/>
<property name= "maxactive" value= "ten"/>
</bean>

<!--TransactionManager--
<bean id= "TransactionManager"
class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "test"/>
</bean>

<bean id= "Basetxproxy"
Class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
Lazy-init= "true" >
<property name= "TransactionManager" >
<ref bean= "TransactionManager"/>
</property>
<property name= "Transactionattributes" >
<props>
<prop key= "*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id= "Test_jdbctemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" >
<property name= "DataSource" >
<ref bean= "Test"/>
</property>
</bean>

<!--Manage Dao---
<bean id= "Testimpdao"
class= "Com.test.dao.TestImpDao" >
<property name= "JdbcTemplate" >
<ref bean= "Test_jdbctemplate"/>
</property>
</bean>

<!--transaction Services--
<bean id= "Testservice" parent= "Basetxproxy" >
<property name= "Target" >
<bean
Class=com.test.service.testserviceimp ">
<property name= "Testimpdao" >
<ref bean= "Testimpdao"/>
</property>
</bean>
</property>
</bean>

Two. Simple description of the implementation class:

Interface for 1.DAO

Public interface Itestdao {

Public void InsertTable1 ();

public void InsertTable2 ();

public void InsertTable3 ();

}

Implementation of the 2.DAO

public class Imptestdao implements itestdao{

public void InsertTable1 () {
This.getjdbctemplate (). Update (Sb.tostring (), Paraobjectarray);
}

public void InsertTable2 () {
This.getjdbctemplate (). Update (Sb.tostring (), Paraobjectarray);
}

public void InsertTable3 () {
This.getjdbctemplate (). Update (Sb.tostring (), Paraobjectarray);
}

}

Interface layer for 3.service:

Public interface Itestservice {
public string Saveoperate (string params);
}

Implementation layer of 4.service

public class Testserviceimp implements itestservice{
Private Testdao Testdao;

public void Settestdao (Testdao Testdao) {
This.testdao=testdao;
}


public void Saveoperate () {
This.testDAO.insertTable1 ();
This.testDAO.insertTable2 ();
This.testDAO.insertTable3 ();
}
}

5. Front-desk calls, such as: Aciton layer L

Public string Saveoperate (string params) {
  string returnstr = "";
  stringbuffer ERRORSB = new StringBuffer ("[");
  try {
    webapplicationcontext appcontext=webapplicationcontextutils.     
                      Getwebapplicationcontext (This.getservlet (). Getservletcontext ());
   itestservice Service = (itestservice) appContext
     .getbean (" Testservice ");
   returnstr = service.saveoperate (params);
  }//If execution fails, the specific exception information is output, and the corresponding transaction is rolled back;
  catch (Exception e) {
   errorsb.append ("{result:error,");
   errorsb.append ("info:" "+ e.getmessage () +" "}");
   errorsb.append ("]");
   returnstr = errorsb.tostring ();
  }

return returnstr;
}

Problems encountered: In the implementation layer of the DAO, the operation of JDBC is thrown into the service implementation layer-that is: ' Testserviceimp ' class,

I wrote Try{}catch () {}, handled the exception in the method, and there was an error that the transaction could not be rolled back.

Note: After debugging, the JDBC operation found in Testdaoimp does not require special handling of the exception when execution occurs.

Instead of throwing exceptions into the service's implementation class, there is no need for the exception to be handled in the service's implementation class.

Instead, throw the exception in the class that calls Serviceimp (for example: the corresponding Aciton), so

<bean id= "Testservice" parent= "Basetxproxy", this proxy class can catch the exception that is thrown by JDBC in order to determine whether the transaction is to be rolled back based on the corresponding exception.

Spring controls COMMIT transaction operations for multiple tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.