KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdori > sql > OjbStoreConnector


1 package org.apache.ojb.jdori.sql;
2 /* Copyright 2002-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import javax.jdo.JDODataStoreException;
18 import javax.jdo.JDOUserException;
19
20 import org.apache.ojb.broker.PersistenceBroker;
21 import org.apache.ojb.broker.PersistenceBrokerFactory;
22 import org.apache.ojb.broker.util.logging.Logger;
23 import org.apache.ojb.broker.util.logging.LoggerFactory;
24
25 import com.sun.jdori.Connector;
26
27 /**
28  * OjbStoreConnector represents a OJB PB connection
29  *
30  * @author Thomas Mahler
31  */

32 class OjbStoreConnector implements Connector
33 {
34     /**
35      * rollback only flag
36      */

37     private boolean rollbackOnlyFlag = false;
38
39     /**
40      * Datasource to which this Connector writes its Message.
41      */

42     private final OjbStorePMF pmf;
43
44     /**
45      * broker represents the backend store.
46      */

47     PersistenceBroker broker = null;
48
49     /**
50      * if true underlying connection can be released
51      */

52     private boolean connectionReadyForRelease = true;
53
54     /**
55      * the logger used for debugging
56      */

57     private Logger logger = LoggerFactory.getLogger("JDO");
58
59     OjbStoreConnector(OjbStorePMF pmf)
60     {
61         this.pmf = pmf;
62     }
63
64
65     /**
66      * @see com.sun.jdori.Connector#begin
67      */

68     public void begin(boolean optimistic)
69     {
70         assertNotRollbackOnly();
71
72         connectionReadyForRelease = false;
73         logger.debug("OjbStoreConnector.begin: connectionReadyForRelease=" + connectionReadyForRelease);
74
75         // obtain a fresh broker and open a tx on it
76
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
77         broker.beginTransaction();
78     }
79
80     /**
81      * @see com.sun.jdori.Connector#beforeCompletion
82      */

83     public void beforeCompletion()
84     {
85         assertNotRollbackOnly();
86         // Nothing to do.
87
}
88
89     /**
90      * @see com.sun.jdori.Connector#flush
91      */

92     public void flush()
93     {
94         assertNotRollbackOnly();
95         logger.debug("OjbStoreConnector.flush: " + "connectionReadyForRelease=" + connectionReadyForRelease);
96         // thma: noop?
97
}
98
99     /**
100      * @see com.sun.jdori.Connector#commit
101      */

102     public synchronized void commit()
103     {
104         assertNotRollbackOnly();
105
106         try
107         {
108             logger.debug("OjbStoreConnector.commit");
109             broker.commitTransaction();
110             broker.close();
111             broker = null;
112         }
113         catch (Exception JavaDoc ex)
114         {
115             throw new OjbStoreFatalInternalException(getClass(), "commit", ex);
116         }
117         finally
118         {
119             connectionReadyForRelease = true;
120         }
121     }
122
123     /**
124      * @see com.sun.jdori.Connector#rollback
125      */

126     public synchronized void rollback()
127     {
128         logger.debug("OjbStoreConnector.rollback");
129
130         if (!rollbackOnlyFlag)
131         {
132             try
133             {
134                 broker.abortTransaction();
135                 broker.close();
136                 broker = null;
137             }
138             catch (Exception JavaDoc ex)
139             {
140                 throw new OjbStoreFatalInternalException(getClass(), "rollback", ex);
141             }
142             finally
143             {
144                 connectionReadyForRelease = true;
145             }
146         }
147     }
148
149     /**
150      * @see com.sun.jdori.Connector#setRollbackOnly
151      */

152     public void setRollbackOnly()
153     {
154         rollbackOnlyFlag = true;
155     }
156
157     /**
158      * @see com.sun.jdori.Connector#getRollbackOnly
159      */

160     public boolean getRollbackOnly()
161     {
162         return rollbackOnlyFlag;
163     }
164
165     private void assertNotRollbackOnly()
166     {
167         if (rollbackOnlyFlag)
168         {
169             throw new JDODataStoreException("Rollback Only !");
170         }
171     }
172
173     /**
174      * Returns the broker.
175      * @return PersistenceBroker
176      */

177     public PersistenceBroker getBroker()
178     {
179         if (broker == null)
180         {
181             throw new JDOUserException("No transaction in progress.");
182         }
183         return broker;
184     }
185
186     /**
187      * Sets the broker.
188      * @param broker The broker to set
189      */

190     public void setBroker(PersistenceBroker broker)
191     {
192         this.broker = broker;
193     }
194
195 }
196
Popular Tags