KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > TransactionImpl


1 //$Id: TransactionImpl.java,v 1.4 2005/07/09 20:04:43 epbernard Exp $
2
package org.hibernate.ejb;
3
4 import javax.persistence.EntityTransaction;
5
6 import org.hibernate.Session;
7 import org.hibernate.Transaction;
8
9 /**
10  * @author Gavin King
11  */

12 public class TransactionImpl implements EntityTransaction {
13
14     private AbstractEntityManagerImpl entityManager;
15     private Transaction tx;
16
17     public TransactionImpl(AbstractEntityManagerImpl entityManager) {
18         this.entityManager = entityManager;
19     }
20
21     private Session getSession() {
22         return entityManager.getSession();
23     }
24
25     public void begin() {
26         if ( tx != null && tx.isActive() ) {
27             throw new IllegalStateException JavaDoc( "Transaction already active" );
28         }
29         tx = getSession().beginTransaction();
30     }
31
32     public void commit() {
33         if ( tx == null || !tx.isActive() ) {
34             throw new IllegalStateException JavaDoc( "Transaction not active" );
35         }
36         tx.commit();
37     }
38
39     public void rollback() {
40         if ( tx == null || !tx.isActive() ) {
41             throw new IllegalStateException JavaDoc( "Transaction not active" );
42         }
43         tx.rollback();
44     }
45
46     public boolean isActive() {
47         return tx != null && tx.isActive();
48     }
49
50 }
51
Popular Tags