KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > tm > DummyTransaction


1 //$Id: DummyTransaction.java,v 1.6 2005/06/03 16:09:33 steveebersole Exp $
2
package org.hibernate.test.tm;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import javax.transaction.HeuristicMixedException JavaDoc;
10 import javax.transaction.HeuristicRollbackException JavaDoc;
11 import javax.transaction.RollbackException JavaDoc;
12 import javax.transaction.Status JavaDoc;
13 import javax.transaction.Synchronization JavaDoc;
14 import javax.transaction.SystemException JavaDoc;
15 import javax.transaction.Transaction JavaDoc;
16 import javax.transaction.xa.XAResource JavaDoc;
17
18 /**
19  * @author Gavin King
20  */

21 public class DummyTransaction implements Transaction JavaDoc {
22     
23     int status;
24     private Connection JavaDoc connection;
25     List JavaDoc synchronizations = new ArrayList JavaDoc();
26     private DummyTransactionManager transactionManager;
27     
28     DummyTransaction(DummyTransactionManager transactionManager) {
29         status = Status.STATUS_NO_TRANSACTION;
30         this.transactionManager = transactionManager;
31     }
32     
33     public void begin() throws SystemException JavaDoc {
34         try {
35             connection = transactionManager.connections.getConnection();
36         }
37         catch (SQLException JavaDoc sqle) {
38             throw new SystemException JavaDoc();
39         }
40         status = Status.STATUS_ACTIVE;
41     }
42
43     public void commit() throws RollbackException JavaDoc, HeuristicMixedException JavaDoc,
44             HeuristicRollbackException JavaDoc, SecurityException JavaDoc,
45             IllegalStateException JavaDoc, SystemException JavaDoc {
46         
47         if (status == Status.STATUS_MARKED_ROLLBACK) {
48             rollback();
49         }
50         else {
51             status = Status.STATUS_PREPARING;
52             
53             for ( int i=0; i<synchronizations.size(); i++ ) {
54                 Synchronization JavaDoc s = (Synchronization JavaDoc) synchronizations.get(i);
55                 s.beforeCompletion();
56             }
57             
58             status = Status.STATUS_COMMITTING;
59             
60             try {
61                 connection.commit();
62                 connection.close();
63             }
64             catch (SQLException JavaDoc sqle) {
65                 status = Status.STATUS_UNKNOWN;
66                 throw new SystemException JavaDoc();
67             }
68             
69             status = Status.STATUS_COMMITTED;
70
71             for ( int i=0; i<synchronizations.size(); i++ ) {
72                 Synchronization JavaDoc s = (Synchronization JavaDoc) synchronizations.get(i);
73                 s.afterCompletion(status);
74             }
75             
76             //status = Status.STATUS_NO_TRANSACTION;
77
transactionManager.endCurrent(this);
78         }
79
80     }
81     
82     public boolean delistResource(XAResource JavaDoc arg0, int arg1)
83             throws IllegalStateException JavaDoc, SystemException JavaDoc {
84         // TODO Auto-generated method stub
85
return false;
86     }
87     
88     public boolean enlistResource(XAResource JavaDoc arg0) throws RollbackException JavaDoc,
89             IllegalStateException JavaDoc, SystemException JavaDoc {
90         // TODO Auto-generated method stub
91
return false;
92     }
93     
94     public int getStatus() throws SystemException JavaDoc {
95         return status;
96     }
97     
98     public void registerSynchronization(Synchronization JavaDoc sync)
99             throws RollbackException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc {
100         synchronizations.add(sync);
101     }
102     
103     public void rollback() throws IllegalStateException JavaDoc, SystemException JavaDoc {
104
105         status = Status.STATUS_ROLLING_BACK;
106
107 // Synch.beforeCompletion() should *not* be called for rollbacks
108
// for ( int i=0; i<synchronizations.size(); i++ ) {
109
// Synchronization s = (Synchronization) synchronizations.get(i);
110
// s.beforeCompletion();
111
// }
112

113         status = Status.STATUS_ROLLEDBACK;
114         
115         try {
116             connection.rollback();
117             connection.close();
118         }
119         catch (SQLException JavaDoc sqle) {
120             status = Status.STATUS_UNKNOWN;
121             throw new SystemException JavaDoc();
122         }
123         
124         for ( int i=0; i<synchronizations.size(); i++ ) {
125             Synchronization JavaDoc s = (Synchronization JavaDoc) synchronizations.get(i);
126             s.afterCompletion(status);
127         }
128         
129         //status = Status.STATUS_NO_TRANSACTION;
130
transactionManager.endCurrent(this);
131     }
132     
133     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc {
134         status = Status.STATUS_MARKED_ROLLBACK;
135     }
136
137     void setConnection(Connection JavaDoc connection) {
138         this.connection = connection;
139     }
140
141     public Connection JavaDoc getConnection() {
142         return connection;
143     }
144 }
145
Popular Tags