0

I have this class TransactionManager:

public class TransactionManager {

    private static ThreadLocal<Transaction> currentTransaction;

    public static Session getCurrentSession() {
        if(currentTransaction == null) {
        currentTransaction.set(
            HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction());
        }
        return HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public static void rollback() {
        currentTransaction.get().rollback();
        HibernateUtil.getSessionFactory().getCurrentSession().close();
        currentTransaction = null;
    }

    public static void beginTransaction() {
        currentTransaction.get().begin();
        // HibernateUtil.getSessionFactory().getCurrentSession().close();
        // currentTransaction = null;
    }

    public static void commit() {
        currentTransaction.get().commit();
        HibernateUtil.getSessionFactory().getCurrentSession().close();
        currentTransaction = null;
    }
}

and the error occurs at beginTransaction(); I've never user a ThreadLocal and I don't really know why I get this error. Without using a ThreadLocal it works fine. The reason I use a ThreadLocal is because I need a different session for every user(I want to create an application like Messenger).

Full error:

java.lang.ClassCastException: 
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction cannot be cast to 
com.util.TransactionManager
Vikdor
  • 23,934
  • 10
  • 61
  • 84
bogdan.rusu
  • 901
  • 4
  • 21
  • 41
  • Can you post the full stack trace instead of the "Full error:"? – Bhesh Gurung Apr 24 '14 at 18:49
  • I fix the problem. I don't need the method beginTransaction() because the transaction is started here : currentTransaction.set( HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction()); Also I forgot to instantiate the currentTransaction : currentTransaction = new ThreadLocal in getCurrentSession method before currentTransaction.set(...) – bogdan.rusu Apr 25 '14 at 08:34

0 Answers0