Hibernate two types of transaction management JDBC and JTA methods. Tell me the difference between the two.
To illustrate the difference between JDBC and JTA mode transaction management:
JDBC transactions are managed by connnection, that is, transaction management is actually in JDBC Connection
Implemented in. Transaction cycles are limited to connection lifecycle
JTA transaction management is implemented by the JTA container, JTA the numerous connection into the current join transaction
Row scheduling to achieve its transactional requirements. The JTA transaction cycle can span multiple JDBC connection lifecycle.
Second, on the basis of understanding JDBC and JTA Affairs, then discuss two kinds of affairs of hibernate
For the hibernate transaction management mechanism based on JDBC transaction, the JDBC Connection that transaction management relies on in the session
, the transaction cycle is limited to the life cycle of the session.
For hibernate based on JTA transactions, JTA transactions can span multiple sessions across.
The different writing in hibernate
The wording of JDBC
public void Saveuser () {
Session session = Sessionfactory.opensession ();
Transaction tx = Session.begintransaction ();
Session.save (user);
Tx.commit ();
Session.close ();
}
Must commit or rollback before session.close ()
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 ();