Hello,
I have implemented an application using Hibernate 2.1.6. The application was working correctly when I decided to upgrade to Hibernate 3.1.2.
The problem is that in some parts of my code I have nested transations, which is a bean that have an insert method and in that insert method I call an insert method of another bean. The insert method of both beans calls session.beginTransaction() and tx.commit() methods.
The problem occurs when calling the second tx.commit() method. An exception is raised "Transaction not successfuly started". It seams that when I first call the tx.commit() method, which occurs in the insert method of the second bean, the transaction is readlly commited, closing the transaction, and when I call the tx.commit() method for the second time, which occurs in the insert method of the first bean, there is no opened transaction, and the exception is raised.
My hibernate.cfg.xml is like the one below:
Code:
<property name="connection.datasource">java:jdbc/TESTEDS</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<!-- Mapping files -->
<mapping resource="Tab1.hbm.xml"/>
<mapping resource="Tab2.hbm.xml"/>
I have an HibernateUtil helper class like the one below:
Code:
package util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static int numOfTransactions = 0;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
The beans' code are below:
Code:
package bean;
import org.hibernate.Session;
import util.HibernateUtil;
public class Tab1 {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void insert() throws Exception{
try
{
Session hibSession = HibernateUtil.currentSession();
Transaction tx = hibSession.beginTransaction();
try {
hibSession.save(this);
Tab2 tab2 = new Tab2();
tab2.setId(new Integer(2));
tab2.setName("test2");
tab2.insert();
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
}
} catch (Exception e) {
throw e;
}
}
}
and
Code:
package bean;
import org.hibernate.Session;
import util.HibernateUtil;
public class Tab2 {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void insert() throws Exception{
try
{
Session hibSession = HibernateUtil.currentSession();
Transaction tx = hibSession.beginTransaction();
try {
hibSession.save(this);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
}
} catch (Exception e) {
throw e;
}
}
}
and in some point of my application i have the following code:
Code:
Tab1 tab1 = new Tab1();
tab1.setId(new Integer(1));
tab1.setName("test1");
tab1.insert();
The application server is jboss-4.0.3SP1, hibernate is not deployed as a service and the database server is MS SQL Server 7.0.
Just to remember, this code works perfectlly well in Hibernate 2.1.6.
I would be very pleased if some could give me a clue of what is wrong.
Thanks in advance.
Fernanda.