Hi
I am trying to use JOTM with hibernate, I am able to get session factory and session objects inside my code but I am geeting:
ERROR: org.hibernate.TransactionException: Unable to locate UserTrasnaction to check status
Please suggest,
I am including the code:
Thanks
Mohit
=================================
package foo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DBTest{
int foo = -1;
static UserTransaction ut;
Session session = null;
static {
try {
Context ctx = new InitialContext();
ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
}catch(Exception e) {
System.out.print("DBTest >> ");
e.printStackTrace();
}
}
public void init(String completion) {
try{
System.out.println("<<< beginning the transaction + session factory created>>>");
System.out.println("foo = "+ foo +" (before completion)");
if (completion != null && completion.equals("commit")) {
ut.begin();
SessionFactory sFBase = new Configuration().
configure("hibernate_foo.cfg.xml").buildSessionFactory();
System.out.println("<<< starting committing the transaction >>>");
session = sFBase.openSession();
System.out.println("before Inserting Record for javatest");
foo.hibernate.com.infosys.Testdata ts = new foo.hibernate.com.infosys.Testdata();
ts.setFoo(new Integer(6));
ts.setId(2);
session.save(ts);
System.out.println("Done updating JavaTest");
ut.commit();
} else {
System.out.println("<<< rolling back the transaction >>>");
//ut.rollback();
}
System.out.println("foo = "+ foo +" (after completion)");
System.out.println("<<< done >>>");
}catch(Exception e) {
System.out.print("DBTest >> ");
e.printStackTrace();
}
}
public String getFoo() { return ""+foo; }
}
==========================================
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/myDB</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="foo/hibernate/com/infosys/Testdata.hbm.xml" />
</session-factory>
</hibernate-configuration>
|