-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate3 - Nested Transactions
PostPosted: Sun Jul 02, 2006 10:36 am 
Newbie

Joined: Sun Jul 02, 2006 8:29 am
Posts: 1
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 02, 2006 9:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Nested transactions are not commonly supported by transaction managers. Hibernate has tightened up some of these areas so as to help users avoid problems that are difficult to track down.

In the case of CMT it will allow you to suspend a transaction (by forcing the container to start a new transaction) that can have the same effect. This extended transaction ability is not exposed to bean managed transactions (that you are effectively using), eg, there is no suspend/resume methods to use (at least not on the surface).

What you need to do is get to the 'TransactionManager' rather than just 'UserTransaction' this is possible as you are using the JTA manager that is being used by the CMT engine is the back ground. In the case of JBoss do a JNDI lookup under "java:/TransactionManager", here is a guideline.

Code:
InitialContext ctx = new InitialContext();
UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");

ut.begin();     // Start first TX
TransactionManager tm = (TransactionManager) ctx.lookup("java:/TransactionManager");
Transaction tx = tm.suspect();

// Nested TX
ut.begin();
//Do something
ut.commit();

// Resume the first TX
tm.resume(tx);

// commit first TX
ut.commit();


Now obviously, CMT makes this much easier (but it not the architecture your using). You can hide much of the working engine in you own utiltiy functions. Also note: the method to get the TransactionManager varies from application servers (as it is not standardised).

Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.