[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Re: Hibernate and JBOSS JTA failed transactions

smithbstl do-not-reply at jboss.com
Fri Sep 29 16:12:22 EDT 2006


ggreaves:  I think with Jboss 4.0.4, you must name your hibernate-service.xml file jboss-service.xml  I am not sure this became necessary but it did.
Here is how I am using JTA Transactions with Hibernate

I created a transaction service helper that basically just does exception handling

package com.stlouiscity.util;
  | import javax.transaction.*;
  | 
  | public class TransactionService {
  |     
  |     /**
  |      * Creates a new instance of TransactionService
  |      */
  |     public TransactionService() {
  |     }
  |     public static UserTransaction getUserTransaction() {
  |         UserTransaction ut = null;
  |         ut = ServiceLocator.getUserTransaction();
  |         return ut;
  |     }
  |     
  |     public static void beginUserTransaction(UserTransaction ut) {
  |         try {
  |             ut.begin();
  |         } catch(NotSupportedException nse) {
  |             throw new TransactionServiceException(nse);
  |         } catch(SystemException se) {
  |             throw new TransactionServiceException(se);
  |         }
  |     }   
  |    
  |     public static void commitUserTransaction(UserTransaction ut) {
  |         try {
  |             ut.commit();
  |         } catch (RollbackException re) {
  |             throw new TransactionServiceException(re);
  |         } catch (HeuristicMixedException hme) {
  |             throw new TransactionServiceException(hme);
  |         } catch (HeuristicRollbackException hre) {
  |             throw new TransactionServiceException(hre);
  |         } catch (java.lang.SecurityException se) {
  |             throw new TransactionServiceException(se);
  |         } catch (java.lang.IllegalStateException ise) {
  |             throw new TransactionServiceException(ise);
  |         } catch (SystemException syse) {
  |             throw new TransactionServiceException(syse);
  |         }
  |     }
  |    
  |     public static void rollbackUserTransaction(UserTransaction ut) {
  |         try {
  |             ut.rollback();
  |         } catch (java.lang.IllegalStateException ise) {
  |             throw new TransactionServiceException(ise);
  |         } catch (java.lang.SecurityException se) {
  |             throw new TransactionServiceException(se);
  |         } catch (SystemException syse) {
  |             throw new TransactionServiceException(syse);
  |         }
  |     }
  | }

Here is the TransactionServiceException Class
public class TransactionServiceException extends RuntimeException {
  |     
  |     /**
  |      * Creates a new instance of TransactionServiceException
  |      */
  |     public TransactionServiceException() {
  |         super();
  |     }
  | 
  |     public TransactionServiceException(String message) {
  |         super(message);
  |     }
  | 
  |     public TransactionServiceException(Throwable cause) {
  |         super(cause);
  |     }
  | 
  |     public TransactionServiceException(String message, Throwable cause) {
  |         super(message, cause);
  |     }
  |     
  | }

Here is the actual DAO logic
package com.stlouiscity.budget.database.dao;
  | 
  | import com.stlouiscity.budget.database.beans.AccountBean;
  | import com.stlouiscity.database.util.HibernateUtil;
  | import com.stlouiscity.util.TransactionService;
  | import com.stlouiscity.util.ServiceLocator;
  | 
  | import java.util.List;
  | import java.util.Iterator;
  | 
  | import javax.transaction.UserTransaction;
  | 
  | import org.hibernate.Session;
  | import org.hibernate.Query;
  | import org.hibernate.Transaction;
  | 
  | public class AccountDAOHibernateImpl implements AccountDAO {
  | 
  |     private long minRange;
  |     private long maxRange;
  |     private static final String HIBERNATE_SESSION_FACTORY 
  |             = "java:comp/env/hibernate/SessionFactory";
  | 
  |     /** Creates a new instance of AccountDAOImpl */
  |     public AccountDAOHibernateImpl() {
  |     }
  |     public void update(AccountBean acctBean) {
  |         Session session = null;
  |         UserTransaction ut = null;
  |         try {
  |             ut = TransactionService.getUserTransaction();
  |             TransactionService.beginUserTransaction(ut);            
  |             session = ServiceLocator.getHibernateSession(HIBERNATE_SESSION_FACTORY);
  |             session.update(acctBean);
  |             TransactionService.commitUserTransaction(ut);
  |         } catch (Exception e) {
  |             System.out.println(e);
  |             TransactionService.rollbackUserTransaction(ut);
  |         } finally {
  |             try {
  |                 if (session !=null) {
  |                     if (session.isOpen()) {
  |                         session.close();
  |                     }
  |                 }
  |             } catch (Exception e) {
  |                 System.out.println(e);
  |             }
  |         }
  |     }

Here is HibernateUtil - I think i copied it from Hibernate In Action
package com.stlouiscity.database.util;
  | import org.hibernate.*;
  | import org.hibernate.cfg.*;
  | 
  | public class HibernateUtil {
  |     
  |     private static final SessionFactory sessionFactory;
  |     /** Creates a new instance of HibernateUtil */
  |     
  |     static {
  |         try {
  |             sessionFactory = new Configuration().configure().buildSessionFactory();
  |         } catch (Throwable ex) {
  |             System.err.println("Initial SessionFactory creation failed." + ex);
  |             throw new ExceptionInInitializerError(ex);
  |         }
  |     }
  |     public static SessionFactory getSessionFactory() {
  |         return sessionFactory;
  |     }
  | }

I hope this help you sort it out

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3975208#3975208

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3975208



More information about the jboss-user mailing list