Using JTA Transaction J Simple method configuration in Hibernate

Source: Internet
Author: User
Tags commit flush

The JTA (Java Transaction API) is an EE solution for transactional services. In essence, it is part of the Java EE model that describes the transaction interface, which developers use directly or through the Java-EE container to ensure that the business logic can run reliably.

The JTA has 3 interfaces, which are usertransaction interfaces, TransactionManager interfaces, and transaction interfaces respectively. These interfaces share common things such as commit () and rollback (), but also include special transaction operations such as suspend (), resume (), and Enlist (), which only appear on specific interfaces to allow some degree of access control in the implementation.

In a system with multiple databases, it is possible for a program to invoke data from several databases, require a distributed transaction, or be prepared to use JTA to manage long transactions across sessions, which requires the use of JTA transactions. The following describes how to configure JTA transactions in a Hibernate configuration file. Set the following in the Hibernate.properties file (cancel the annotation character "#" of the configuration line where the Jtatransactionfactory is located):

Hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory
# Hibernate.transaction.factory_class Org.hibernate.transaction.JDBCTransactionFactory or in the Hibernate.cfg.xml file is configured as follows:

<session-factory>
.....
<property name= "Hibernate.transaction.factory_class" >
Org.hibernate.transaction.JTATransactionFactory
</property>
......
</session-factory> Below is an example of applying a JTA transaction:

Javax.transaction.UserTransaction tx = NULL;
tx = new InitialContext (). Lookup ("Javax.transaction.UserTransaction");
Tx.begin ();
Session S1 = Sf.opensession ();
......
S1.flush (); S1.close ();
Session S2 = Sf.opensession ();
......
S2.flush (); S2.close ();
Tx.commit ();

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.