Implementation of JTA transactions with Tyrex integrated hibernate and JDBC in unmanaged environments
The recent integration of hibernate into the company's products, the company's products using Tyrex to provide JTA transaction support, its own based on JDBC implementation of a set of O/R Mapping, and embedded tomcat, so the transaction part of its own implementation, and Tomcat's environment, independent of For JTA in unmanaged environments, and spring is similar to JOTM integration. The normal use of JDBC is as follows:
Boolean transaction = FALSE;
Connection = Connection = null;
try {
Transaction = Transactionutil.begin ();
Connection = Connectionfactory.getconnection ("localoracle");
JDBC actions are performed here
catch (Exception e) {
Transactionutil.rollback ();
finally {
if (connection!= null) {
Connection.close ();
}
Transactionutil.commit (transaction);
}
In order to integrate Hibernate has implemented a connectionprovider:
Package com.company.bpms.client.hibernate;
Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.Properties;
Import org.hibernate.HibernateException;
Import Org.hibernate.connection.ConnectionProvider;
Import Com.company.entity.ConnectionFactory;
Import com.company.entity.GenericEntityException;
Import Com.company.util.Debug;
public class Bpconnectionprovider implements ConnectionProvider {
Private String helpername = null;
Private static final String module = BPConnectionProvider.class.getName ();
Public String Gethelpername () {
return helpername;
}
public void Sethelpername (String helpername) {
This.helpername = Helpername;
}
public void Configure (properties properties) throws Hibernateexception {
if (Helpername = = null) {
Helpername = (String) properties.get ("Bpms.helpername");
}
if (Helpername = = null) {
throw new Hibernateexception ("Helpername Not Configured.");
}
Debug.loginfo ("Bpms.helpername:" + helpername, module);
}
Public Connection getconnection () throws SQLException {
try {
Return connectionfactory.getconnection (Helpername);
catch (Genericentityexception e) {
Debug.logerror (E, module);
throw new SQLException (E.getmessage ());
}
}
public void CloseConnection (Connection Connection) throws SQLException {
Connection.close ();
}
public Boolean supportsaggressiverelease () {
return false;
}
public void Close () throws Hibernateexception {
}
}
There is also a Hibernate.transaction.manager_lookup_class class:
Package com.company.bpms.client.hibernate;
Import java.util.Properties;
Import Javax.transaction.TransactionManager;
Import org.hibernate.HibernateException;
Import Org.hibernate.transaction.TransactionManagerLookup;
Import Com.company.entity.TransactionFactory;
public class Tyrextransactionmanagerlookup implements Transactionmanagerlookup {
Public TransactionManager Gettransactionmanager (Properties arg0) throws Hibernateexception {
TransactionManager manager = Transactionfactory.gettransactionmanager ();
if (manager = = null) {
throw new Hibernateexception ("Could not obtain Tyrex transaction manager instance");
}
Return manager;
}
Public String Getusertransactionname () {
return null;
}
}
A Hibernate help class:
Package com.company.bpms.client.hibernate;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Com.company.util.Debug;
public class Hibernateutil {
private static Sessionfactory sessionfactory;
Private static final String module = HibernateUtil.class.getName ();
public static Sessionfactory Getsessionfactory () throws Exception {
if (sessionfactory = = null) {
Synchronized (Hibernateutil.class) {
if (sessionfactory = = null) {
try {
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
catch (Exception e) {
Debug.logerror (E, module);
Throw e;
}
}
}
}
return sessionfactory;
}
}
Configuration file:
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Hibernate.connection.provider_class" > Com.company.bpms.client.hibernate.BPConnectionProvider </property>
<property name= "Bpms.helpername" >localoracle</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.Oracle9Dialect</property>
<property name= "Hibernate.show_sql" >true</property>
<property name= "Hibernate.transaction.factory_class" >org.hibernate.transaction.jtatransactionfactory</ Property>
<property name= "Hibernate.transaction.manager_lookup_class" > Com.company.bpms.client.hibernate.TyrexTransactionManagerLookup </property>
<!--
<property name= "Hibernate.hbm2ddl.auto" >create</property>
-->
<mapping resource= "Test/entity/user.hbm.xml"/>
<mapping resource= "Test/entity/userprofile.hbm.xml"/>
</session-factory>
Sample code:
Boolean transaction = FALSE;
Org.hibernate.Session session1 = null;
Org.hibernate.Session session2 = null;
try {
Transaction = Transactionutil.begin ();
Session1 = Hibernateutil.getsessionfactory (). Getcurrentsession ();
Session1 = Hibernateutil.getsessionfactory (). Opensession ();
User user = new user ();
User.setusername ("AAA");
Session1.save (user);
Session1.flush ();
Session1.close ();
Bugtypeoperate.createbugtype (Utilmisc.tomap ("Bugtypeid", User.getuserid (), "Bugtypename", "BBB"));
Session2 = Hibernateutil.getsessionfactory (). Getcurrentsession ();
Session2 = Hibernateutil.getsessionfactory (). Opensession ();
UserProfile profile = new UserProfile ();
Profile.setuserid (User.getuserid ());
Profile.setcompanyaddress ("aaaaaaa");
Session2.save (profile);
Session2.flush ();
Session2.close ();
Out.print ("OK");
catch (Exception e) {
Debug.logerror (e);
Out.print ("Error");
Transactionutil.rollback ();
finally {
/*
if (session1!= null && session1.isopen ()) {
Debug.loginfo ("-------->colse session1");
Session1.close ();
Debug.loginfo ("--------> Finish close Session1");
}
if (session2!= null && session2.isopen ()) {
Debug.loginfo ("-------->colse session2");
Session2.close ();
Debug.loginfo ("--------> Finish close Session2");
*/
Transactionutil.commit (transaction);
}
Using the Getcurrentsession () method, this session is automatically closed when the transaction is last committed, without the need to manually close the session. If you use the Opensession () method, you need to manually close the open session. The Bugtypeoperate.createbugtype () method is our own JDBC implementation and can now be put into a transaction with hibernate.