KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > transaction > JOTMTransactionFactoryImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: JOTMTransactionFactoryImpl.java,v 1.5 2007/01/07 06:14:21 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db.transaction;
23
24 import java.sql.Connection JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.naming.NamingException JavaDoc;
29 import javax.transaction.SystemException JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import org.objectweb.transaction.jta.TMService;
34 import org.opensubsystems.core.error.OSSDatabaseAccessException;
35 import org.opensubsystems.core.error.OSSException;
36 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl;
37 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl;
38 import org.opensubsystems.core.persist.db.connectionpool.XAPoolDatabaseConnectionFactoryImpl;
39 import org.opensubsystems.core.util.GlobalConstants;
40 import org.opensubsystems.core.util.Log;
41
42 /**
43  * Transaction factory using JOTM transaction manager from Objectweb
44  * (http://www.objectweb.org/jotm/index.html)
45  *
46  * @version $Id: JOTMTransactionFactoryImpl.java,v 1.5 2007/01/07 06:14:21 bastafidli Exp $
47  * @author Miro Halas
48  * @code.reviewer Miro Halas
49  * @code.reviewed 1.3 2006/05/15 05:12:15 bastafidli
50  */

51 public class JOTMTransactionFactoryImpl extends DatabaseTransactionFactoryImpl
52 {
53    // Attributes ///////////////////////////////////////////////////////////////
54

55    /**
56     * Instance of JOTM transaction manager.
57     */

58    protected TMService m_tmJotm;
59    
60    /**
61     * True if transaction factory is started.
62     */

63    protected boolean m_bStarted = false;
64
65    // Cached values ////////////////////////////////////////////////////////////
66

67    /**
68     * Logger for this class
69     */

70    private static Logger JavaDoc s_logger = Log.getInstance(JOTMTransactionFactoryImpl.class);
71
72    // Constructors /////////////////////////////////////////////////////////////
73

74    /**
75     * Create new transaction factory.
76     */

77    public JOTMTransactionFactoryImpl(
78    )
79    {
80       super();
81
82       start();
83    }
84    
85    /**
86     * {@inheritDoc}
87     */

88    public TransactionManager JavaDoc getTransactionManager(
89    )
90    {
91       if (!m_bStarted)
92       {
93          start();
94       }
95
96       return m_tmJotm.getTransactionManager();
97    }
98
99    /**
100     * {@inheritDoc}
101     */

102    public UserTransaction JavaDoc requestTransaction(
103    ) throws OSSException
104    {
105       if (!m_bStarted)
106       {
107          start();
108       }
109
110       UserTransaction JavaDoc transaction;
111       
112       transaction = m_tmJotm.getUserTransaction();
113       if (s_bTransactionMonitor)
114       {
115          transaction = new DelegatingUserTransaction(transaction);
116       }
117       try
118       {
119          transaction.setTransactionTimeout(s_iTransactionTimeout);
120       }
121       catch (SystemException JavaDoc seExc)
122       {
123          throw new OSSDatabaseAccessException("Cannot set transaction timeout", seExc);
124       }
125
126       return transaction;
127    }
128    
129    /**
130     * {@inheritDoc}
131     */

132    public void reset(
133    ) throws OSSException
134    {
135       // The only thing we can do is reset the thread local again. This method
136
// should be used only for testing
137
s_logger.severe("About to reset state of transaction manage." +
138                       " Hope you know what you are doing.");
139       // Not sure how to reset JOTM so stop and start it
140
stop();
141       start();
142    }
143
144    /**
145     * {@inheritDoc}
146     */

147    public void stop(
148    ) throws OSSException
149    {
150       if (m_tmJotm != null)
151       {
152          s_logger.fine("Stopping JoTM transaction manager.");
153          m_tmJotm.stop();
154          m_bStarted = false;
155          s_logger.fine("JoTM transaction manager is stopped.");
156       }
157    }
158
159    // Helper methods ///////////////////////////////////////////////////////////
160

161    /**
162     * Start the transaction manager.
163     */

164    protected void start(
165    )
166    {
167       if (!m_bStarted)
168       {
169          // Create local instance of JOTM not bound to any registry since we don't
170
// need any
171
s_logger.fine("Starting JoTM transaction manager.");
172          
173          try
174          {
175             m_tmJotm = new org.objectweb.jotm.Jotm(true, false);
176    
177 // Context ictx = null;
178
// String userTransactionName = "javax.transaction.UserTransaction";
179
// String transactionManagerName = "javax.transaction.TransactionManager";
180

181 // This is causing exception with JOTM > 1.4.2 (tested with 1.5.3)
182
// ictx = new InitialContext();
183
//
184
// if (userTransactionName != null)
185
// {
186
// ictx.rebind(userTransactionName, m_tmJotm.getUserTransaction());
187
// s_logger.finer("UserTransaction object bound in JNDI with name "
188
// + userTransactionName);
189
// }
190
//
191
// if (transactionManagerName != null)
192
// {
193
// ictx.rebind(transactionManagerName, m_tmJotm.getTransactionManager());
194
// s_logger.finer("TransactionManager object bound in JNDI with name "
195
// + transactionManagerName);
196
// }
197
}
198          catch (NamingException JavaDoc nmeExc)
199          {
200             s_logger.log(Level.SEVERE, "Cannot create instance of JOTM", nmeExc);
201             throw new RuntimeException JavaDoc("Cannot create instance of JOTM", nmeExc);
202          }
203          
204          m_bStarted = true;
205          
206          s_logger.fine("JoTM is running...");
207       }
208       else
209       {
210          s_logger.fine("JoTM is already running...");
211       }
212    }
213    
214    /**
215     * {@inheritDoc}
216     */

217    protected Connection JavaDoc requestTransactionalConnection(
218       boolean bAutoCommit,
219       String JavaDoc strDataSourceName,
220       String JavaDoc strUser,
221       String JavaDoc strPassword,
222       DatabaseConnectionFactoryImpl connectionFactory
223    ) throws OSSDatabaseAccessException
224    {
225       if (GlobalConstants.ERROR_CHECKING)
226       {
227          // JOTM currently works only with XAPool because JOTM can manage
228
// only XAResources and only XAPool represents database connections
229
// as XAResources.
230
// Lets check if it is so
231
assert connectionFactory instanceof XAPoolDatabaseConnectionFactoryImpl
232                 : "JOTM can manage only XAPool connections at this time.";
233       }
234
235       // XAPool created connection is always already connected to JOTM therefore
236
// nothing neeeds to be done, just call the appropriate version of the method
237
return super.requestTransactionalConnection(bAutoCommit, strDataSourceName,
238                                                   strUser, strPassword,
239                                                   connectionFactory);
240    }
241
242    /**
243     * {@inheritDoc}
244     */

245    protected void returnTransactionalConnection(
246       Connection JavaDoc cntDBConnection,
247       DatabaseConnectionFactoryImpl connectionFactory
248    )
249    {
250       if (GlobalConstants.ERROR_CHECKING)
251       {
252          // JOTM currently works only with XAPool because JOTM can manage
253
// only XAResources and only XAPool represents database connections
254
// as XAResources.
255
// Lets check if it is so
256
assert connectionFactory instanceof XAPoolDatabaseConnectionFactoryImpl
257                 : "JOTM can manage only XAPool connections at this time.";
258       }
259
260       // Since JOTM and XAPool are already integrated just call the base class
261
super.returnTransactionalConnection(cntDBConnection, connectionFactory);
262    }
263 }
264
Popular Tags