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 ();