KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > transaction > base > TransactionManagerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.transaction.base;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import javax.transaction.*;
27 import oracle.toplink.essentials.internal.ejb.cmp3.base.ExceptionFactory;
28 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.DataSourceImpl;
29
30 /**
31  * Implementation of JTA Transaction manager class.
32  *
33  * Currently support is limited to enlisting a single tx data source
34  */

35 public class TransactionManagerImpl implements TransactionManager, UserTransaction {
36     // Not null when a transaction is active
37
TransactionImpl tx;
38
39     /************************/
40     /***** Internal API *****/
41     /************************/
42     private void debug(String JavaDoc s) {
43         System.out.println(s);
44     }
45
46     /*
47      * Used to create the single instance
48      */

49     public TransactionManagerImpl() {
50         this.tx = null;
51     }
52
53     /*
54      * Return true if a transaction has been explicitly begun
55      */

56     public boolean isTransactionActive() {
57         return tx != null;
58     }
59
60     /*
61      * Return a Connection if a transaction is active, otherwise return null
62      */

63     public Connection JavaDoc getConnection(DataSourceImpl ds, String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
64         return (tx == null) ? null : tx.getConnection(ds, user, password);
65     }
66
67     /************************************************************/
68     /***** Supported TransactionManager/UserTransaction API *****/
69     /************************************************************/
70     public void begin() throws NotSupportedException, SystemException {
71         debug("Tx - begin");
72
73         if (isTransactionActive()) {
74             throw new ExceptionFactory().txActiveException();
75         }
76
77         // New transaction created by virtue of Transaction existence
78
tx = new TransactionImpl();
79     }
80
81     public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException JavaDoc, IllegalStateException JavaDoc, SystemException {
82         debug("Tx - commit");
83
84         if (!isTransactionActive()) {
85             throw new ExceptionFactory().txNotActiveException();
86         }
87         try{
88             tx.commit();
89         }finally{
90             tx = null;
91         }
92     }
93
94     public int getStatus() throws SystemException {
95         return (!isTransactionActive()) ? Status.STATUS_NO_TRANSACTION : tx.getStatus();
96     }
97
98     public Transaction getTransaction() throws SystemException {
99         return tx;
100
101     }
102
103     public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException {
104         debug("Tx - rollback");
105
106         if (!isTransactionActive()) {
107             throw new ExceptionFactory().txNotActiveException();
108         }
109         try{
110             tx.rollback();
111         }finally{
112             tx = null;
113         }
114     }
115
116     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException {
117         debug("Tx - rollback");
118
119         if (!isTransactionActive()) {
120             throw new ExceptionFactory().txNotActiveException();
121         }
122         tx.setRollbackOnly();
123     }
124
125     /****************************************************************/
126     /***** NOT supported TransactionManager/UserTransaction API *****/
127     /****************************************************************/
128     public Transaction suspend() throws SystemException {
129         return null;
130     }
131
132     public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException JavaDoc, SystemException {
133         // Do nothing
134
}
135
136     public void setTransactionTimeout(int i) throws SystemException {
137         // Do nothing
138
}
139 }
140
Popular Tags