Java transaction note

Source: Internet
Author: User
Summary of ibatis, connection pool, and transactions: 1. ibatis works with proxool. Session. close () does not actually call connection. close (). proxool replaces the connection by entrusting a dynamic proxy. close () implementation, must call connection. close () can be recycled, rather than physical disconnection. Therefore, after using the proxool connection pool, the session is required every time the session is used up. getconnection. close (); 2. ibatis 3 + spring transactions are not internally supported. Therefore, life-style transactions cannot be used by default in spring + ibatis 3. You need to write an extra transactionmanager implementation for ibatis 3 declarative transactions. A similar implementation was provided to Chris last time. 3. the MySQL InnoDB engine can support transaction and cascading reference integrity. 4. JDBC 3 supports savepoints () rollback in stages. e.g.: Savepoint SP1 = connection. setsavepoint ("savepoint 1 "); ... Connection. rollback (SP1); Java. SQL. savepoint must exist, that is, JDK version 1.4 +; MySQL InnoDB engine from MySQL 5.0.3 support savepoint (see: http://dev.mysql.com/doc/refman/5.0/en/savepoint.html); 5. spring-managed transactions support: the same connection of database transactions, cross-Distributed Database jtx distributed transactions; 6. simple Meanings of the transaction level of spring declarative transactions: Propagation_required: inherits the current context transaction. If there is no transaction in the current context, a new transaction is created. This is the most common choice. Propagation_supports: supports the current context transaction. If there is no transaction in the current context, it is executed in non-transaction mode; that is, the transaction is optional; Propagation_mandatory: the current context must have a transaction; if there is no transaction in the current context, an exception is thrown; Propagation_requires_new: ignores the current context transaction. New transactions are always created and the context transaction is suspended until the end of the new transaction. That is, the success or failure of the external transaction does not affect the current transaction result; Propagation_not_supported: fooled the current context transaction, in non-transaction mode; Propagation_never: execution is not allowed in the transaction context. If a transaction exists in the current context, an exception is thrown; Propagation_nested: Embedded transactions (no equivalent concept in EJB), only supported in some transactionmanagers. Only supported in JDBC 3.0 + datasourcetransactionmanager; If a transaction exists, it is executed in the nested transaction. If no transaction exists, perform a similar operation as propagation_required. The first six policies are similar to ejb cmt, and the seventh (propagation_nested) is a special variable provided by spring. It requires the Transaction Manager or the JDBC 3.0 savepoint API to provide nested transaction behaviors (such as spring's datasourcetransactionmanager) 7. Notes for ibatis 3 transaction operations: 1). Enable session: sqlsession session = sessionfactory. opensession (transactionisolationlevel. read_committed ); 2). Set non-automatic commit (otherwise each statement is treated as a transaction): Session. getconnection (). setautocommit (false ); 3). Considerations for submission: Session. getconnection (). Commit (); the use of session. Commit () is invalid, which is very strange. It is estimated that session. rollback () is also invalid; 4). If some statements require batch operation (e.g.: addbatch): You can use the same connection to create a new session: Sqlsession session1 = sessionfactory. opensession (executortype. batch, session. getconnection ()); However, there is a strange thing: The test finds that the efficiency of JDBC batch operations is not as good as direct insertion; others also provide feedback on the Internet; 8. in summary, it is the best choice to adopt spring's life-style transactions. The most flexible and transparent support for local (same database connection) and distributed transactions (implemented through jtx distributed transactions); see: http://www.javaeye.com/topic/35907

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.