Java transaction (8), java transaction

Source: Internet
Author: User

Java transaction (8), java transaction

I. Preface:

In the previous blog, we used jotm to implement distributed transactions. This article uses atomikos.

The basic code is the same, that is, the configuration is slightly different.


Ii. Code implementation:

1. code structure:


2. configuration file: ApplicationContext. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.sprin Gframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <! -- Database 1 --> <bean id = "db1" class = "com. atomikos. jdbc. simpleDataSourceBean "init-method =" init "destroy-method =" close "> <property name =" uniqueResourceName "> <value> mysql/main </value> </property> <property name = "xaDataSourceClassName"> <value> com. mysql. jdbc. jdbc2.optional. mysqlXADataSource </value> </property> <property name = "xaDataSourceProperties"> <value> URL = jdbc: mysql: // localhost: 3306/user? UseUnicode = true & characterEncoding = UTF-8; user = root; password = root </value> </property> <property name = "exclusiveConnectionMode"> <value> true </value> </property> <property name = "connectionPoolSize"> <value> 3 </value> </property> <property name = "validatingQuery"> <value> SELECT 1 </value> </property> </bean> <! -- Database 2 --> <bean id = "db2" class = "com. atomikos. jdbc. simpleDataSourceBean "init-method =" init "destroy-method =" close "> <property name =" uniqueResourceName "> <value> mysql/secondary </value> </property> <property name = "xaDataSourceClassName"> <value> com. mysql. jdbc. jdbc2.optional. mysqlXADataSource </value> </property> <property name = "xaDataSourceProperties"> <value> URL = jdbc: mysql: // localhost: 3306/log? UseUnicode = true & characterEncoding = UTF-8; user = root; password = root </value> </property> <property name = "exclusiveConnectionMode"> <value> true </value> </property> <property name = "connectionPoolSize"> <value> 3 </value> </property> <property name = "validatingQuery"> <value> SELECT 1 </value> </property> </bean> <bean id = "userTransactionManager" init-method = "init" destroy-method = "close" class = "com. atomikos. ica Tch. jta. userTransactionManager "> <property name =" forceshudown "value =" true "/> </bean> <bean id =" userTransactionImp "class =" com. atomikos. icatch. jta. userTransactionImp "> <property name =" transactionTimeout "value =" 300 "/> </bean> <bean id =" jtaTransactionManager "class =" org. springframework. transaction. jta. jtaTransactionManager "> <property name =" transactionManager "ref =" userTransactionManager "/> <pro Perty name = "userTransaction" ref = "userTransactionImp"/> <property name = "allowCustomIsolationLevels" value = "true"/> </bean> <! -- Configure the transaction propagation feature --> <tx: advice id = "txAdvice" transaction-manager = "jtaTransactionManager"> <tx: attributes> <tx: method name = "save *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "add *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "create *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "insert *" propagation = "REQUIRED" rollback-for = "Exception"/> <Tx: method name = "del *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "update *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "*" read-only = "true"/> </tx: attributes> </tx: advice> <! -- Transaction Management --> <aop: config proxy-target-class = "true"> <aop: advisor pointcut = "execution (* com. zdp. service .. *. *(..)) "advice-ref =" txAdvice "/> </aop: config> <bean id =" userDao "class =" com. zdp. dao. userDao "> <property name =" dataSource "ref =" db1 "/> </bean> <bean id =" logDao "class =" com. zdp. dao. logDao "> <property name =" dataSource "ref =" db2 "/> </bean> <bean id =" userService "class =" com. zdp. service. userService "> <property name =" userDao "ref =" userDao "/> <property name =" logDao "ref =" logDao "/> </bean> </beans>
For other code, see the previous blog.

Source code download: http://download.csdn.net/detail/zdp072/7950391



In java transactions

Here is a detailed description of mianshi.fenzhi.com/post/522.html !!

Java transactions

Java Transaction Processing

Generally, the J2EE Application Server supports JDBC transactions, JTA (Java Transaction API) transactions, and container management transactions. Generally, it is better not to use the above three transaction types in the program at the same time, such as nesting JDBC transactions in JTA transactions. Second, the transaction should be completed in the shortest time. Do not use the transaction in different methods. The following lists two transaction processing methods.

1. Use JDBC for transaction processing in JavaBean
In JDBC, how does one combine multiple SQL statements into one transaction? In JDBC, when a Connection object is opened, the default mode is auto-commit. Each SQL statement is treated as a transaction, that is, each statement is automatically confirmed by the transaction. In order to combine multiple SQL statements into a transaction, We need to block the auto-commit mode. If the commit () method is not called after the auto-commit mode is disabled, the SQL statement will not be confirmed by the transaction. All SQL statements after the last call of the commit () method are confirmed when the method commit () is called.

Public int delete (int sID ){
Dbc = new DataBaseConnection ();
Connection con = dbc. getConnection ();
Try {
Con. setAutoCommit (false); // change the default commit method of JDBC transactions
Dbc.exe cuteUpdate ("delete from bylaw where ID =" + sID );
Dbc.exe cuteUpdate ("delete from bylaw _ content where ID =" + sID );
Dbc.exe cuteUpdate ("delete from bylaw _ affix where bylawid =" + sID );
Con. commit (); // submit a JDBC transaction
Con. setAutoCommit (true); // restore the default commit Method for JDBC transactions
Dbc. close ();
Return 1;
}
Catch (Exception exc ){
Con. rollBack (); // roll back JDBC transactions
Exc. printStackTrace ();
Dbc. close ();
Return-1;
}
}

2. JTA transactions in SessionBean
JTA is a J2EE solution for transaction services. Essentially, it is part of the J2EE model that describes the transaction interface (such as the UserTransaction interface, which developers directly use or use through the J2EE container to ensure that the business logic can run reliably. JTA has three main interfaces: UserTransaction interface, TransactionManager interface, and Transaction interface. These interfaces share common transaction operations, such as commit () and rollback (), but also include special transaction operations, such as suspend (), resume (), and enlist (), they only appear on specific interfaces to allow a certain degree of access control in the implementation. For example, UserTransaction can perform Transaction Division and basic transaction operations, while TransactionManager can perform context management.

The application can call the UserTransaction. begin () side ...... the remaining full text>

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.