JDBC Transaction
String url= "Jdbc:sqlserver://localhost:1433;databasename=test2";
String user= "SA";
String password= "Sapassword";
Try
{
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=drivermanager.getconnection (URL, USER, PASSWORD);
Statement s = conn.createstatement ();
ResultSet rs = s.executequery ("SELECT * from T_user");
while (Rs.next ())
{
System.out.println (rs.getstring ("name") + "... Age:" +rs.getstring ("Age");
}
Rs.close ();
S.close ();
Conn.close ();
} catch (ClassNotFoundException | SQLException e)
{
E.printstacktrace ();
}
Using JDBC transactions in Hibernate
Hibernate provides a lightweight encapsulation of JDBC, which itself does not have transactional capabilities at design time. Hibernate encapsulates the underlying jdbctransaction or jtatransaction, and then sets the outer shell of the transaction and session, in fact, by delegating the underlying JDBC or JTA to handle the transaction function.
To use a transaction in Hibernate, you can specify the use of Jdbctransaction or jtatransaction in its configuration file. In Hibernate.properties, look for the "transaction.factory_class" keyword to get the following configuration:
# Hibernate.transaction.factory_class Org.hibernate.transaction.JTATransactionFactory # Hibernate.transaction.factory_class Org.hibernate.transaction.JDBCTransactionFactory |
Hibernate's transaction factory class can be set to Jdbctransactionfactory or Jtatransactionfactory. If not configured, Hibernate will assume that the transaction used by the system is a JDBC transaction.
In the JDBC commit mode, if the database connection is autocommit (auto commit mode), the transaction is committed after each SQL statement execution, and if there is a task after the commit, a new transaction begins.
Hibernate in session control, after obtaining the database connection, immediately cancel the auto-commit mode, that is, hibernate in a BeginTransaction () method to execute the session, The JDBC layer's Setautocommit (false) is automatically called. If you want to provide your own database connection and use your own SQL statements, in order to implement a transaction, you must first turn off Autocommit (Setautocommit (false)) and commit the transaction at the end of the transaction.
Using JDBC transactions is the simplest way to implement transaction management, and Hibernate's encapsulation of JDBC transactions is simple. Here is an example of using JDBC transactions in Hibernate:
try { Session session = Hibernateutil.currentsession (); Transaction tx = Session.begintransaction (); By default, a JDBC thing is turned on for (int i=0; i<10; i++) { Student stu = new Student (); Stu.setname ("Student" + i); Session.save (Stu); } Tx.commit (); Commit a transaction Session.close (); } catch (Exception e) {... Tx.rollback (); Transaction rollback } |
Using JTA transactions in Hibernate
JTA (Java Transaction API) is the EE solution for transactional services. Essentially, it is part of the EE model that describes the transactional interface that the developer uses directly or through the Java EE container to ensure that the business logic runs reliably.
The JTA has 3 interfaces, each of which is the UserTransaction interface, the TransactionManager interface, and the transaction interface. These interfaces share common things operations, such as commit () and rollback (), but also contain special transactional operations such as suspend (), resume (), and Enlist (), which appear only on specific interfaces to allow some degree of access control in the implementation.
In a system with multiple databases, a program may invoke data from several databases, require a distributed transaction, or be prepared to use JTA to manage long transactions across sessions, then a JTA transaction is required. Here's how to configure the JTA transaction in Hibernate's configuration file. Set the following in the Hibernate.properties file (uncomment "#" 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, configure the following in the Hibernate.cfg.xml file:
<session-factory> ..... <property name= "Hibernate.transaction.factory_class" > Org.hibernate.transaction.JTATransactionFactory </property> ...... </session-factory> |
Here 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 ();
|
Transaction type JDBC,HIBERNATE,JTA Transaction