KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > mapper > rdb > JDBCConnectionHolder


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.mapper.rdb;
19
20 import org.objectweb.perseus.persistence.api.ConnectionHolder;
21 import org.objectweb.perseus.persistence.api.PersistenceException;
22 import org.objectweb.perseus.persistence.api.WorkingSet;
23 import org.objectweb.util.monolog.api.BasicLevel;
24 import org.objectweb.util.monolog.api.Logger;
25 import org.objectweb.jorm.api.PMapper;
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.speedo.pm.api.ProxyManager;
28 import org.objectweb.speedo.workingset.api.Transaction;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 /**
34  * Is an holder of JDBC connection. It can works in managed or non-managed
35  * transactional environnement.
36  *
37  * @author S.Chassande-Barrioz
38  */

39 public class JDBCConnectionHolder implements ConnectionHolder {
40
41     /**
42      * the mapper allocating JDBC connections
43      */

44     protected PMapper mapper;
45
46     /**
47      * The workingSet attached to this connection holder
48      */

49     protected WorkingSet workingSet;
50
51     /**
52      * The SQL connection held. Can be null if no connectioin has been used
53      */

54     protected Connection JavaDoc connection;
55
56     /**
57      * Indicates the access through the connection must be done inside a
58      * transaction.
59      */

60     protected boolean transactional = false;
61
62     /**
63      * Indicates if the transaction demarcations must tbe done by the
64      * ConnectionHolder or there are already done by an environment
65      * (an Application Server for example).
66      */

67     protected boolean txManagedExternaly = false;
68
69     protected Logger logger;
70
71     public JDBCConnectionHolder(PMapper mapper, Logger logger) {
72         this.mapper = mapper;
73         this.logger = logger;
74     }
75
76     /**
77      * Primitive method demarcating the begining of a local transaction on the
78      * current connection.
79      */

80     protected void beginLocalTransaction() throws PersistenceException {
81         if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
82             logger.log(BasicLevel.DEBUG,
83                     "Begin a local transaction on the connection: " + connection);
84         }
85         try {
86             connection.setAutoCommit(false);
87         } catch (SQLException JavaDoc e) {
88             throw new PersistenceException(e);
89         }
90     }
91
92     /**
93      * primitive method demarcating the end of a local transaction by a commit
94      */

95     protected void commitLocalTransaction() throws PersistenceException {
96         if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
97             logger.log(BasicLevel.DEBUG,
98                     "Commit the local transaction on the connection: " + connection);
99         }
100         try {
101             connection.commit();
102             connection.setAutoCommit(true);
103         } catch (SQLException JavaDoc e) {
104             throw new PersistenceException(e);
105         }
106     }
107
108     /**
109      * primitive method demarcating the end of a local transaction by a rollback
110      */

111     protected void rollbackLocalTransaction() throws PersistenceException {
112         if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
113             logger.log(BasicLevel.DEBUG,
114                     "Rollback the local transaction on the connection: " + connection);
115         }
116         try {
117             connection.rollback();
118             connection.setAutoCommit(true);
119         } catch (SQLException JavaDoc e) {
120             throw new PersistenceException(e);
121         }
122     }
123
124     // IMPLEMENTATION OF THE ConnectionHolder INTERFACE //
125
//--------------------------------------------------//
126

127     public WorkingSet getWorkingSet() {
128         return workingSet;
129     }
130
131     public void bindWorkingSet(WorkingSet workingSet) {
132         this.workingSet = workingSet;
133     }
134
135     public Object JavaDoc getCHConnectionForRead() throws PersistenceException {
136         return getCHConnectionForWrite();
137     }
138
139     public Object JavaDoc getCHConnectionForWrite() throws PersistenceException {
140         if (connection == null) {
141             Object JavaDoc cs = null;
142             if (workingSet instanceof Transaction) {
143                 Transaction tx = (Transaction) workingSet;
144                 cs = ((ProxyManager) tx.getPersistenceManager())
145                         .getConnectionSpec();
146                 txManagedExternaly = tx.isManagedEnv();
147             }
148             try {
149                 if (cs == null) {
150                     connection = (Connection JavaDoc) mapper.getConnection();
151                 } else {
152                     connection = (Connection JavaDoc) mapper.getConnection(cs);
153                 }
154             } catch (PException e) {
155                 throw new PersistenceException("Impossible to fetch a SQL connection", e);
156             }
157             if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
158                 logger.log(BasicLevel.DEBUG, "Allocate a connection: " + connection);
159             }
160             if (transactional && !txManagedExternaly) {
161                 beginLocalTransaction();
162             }
163         }
164         return connection;
165     }
166
167     /**
168      * If there is no connection already allocated, the transaction being will
169      * be during the next connection request (getConnectionFor...(...)
170      *
171      * @throws org.objectweb.perseus.persistence.api.PersistenceException
172      */

173     public void begin() throws PersistenceException {
174         transactional = true;
175         if (connection != null) {
176             beginLocalTransaction();
177         }
178     }
179
180     public void commitCH() throws PersistenceException {
181         try {
182             if (connection != null && !txManagedExternaly && transactional) {
183                 commitLocalTransaction();
184             }
185         } finally {
186             transactional = false;
187         }
188     }
189
190     public void rollbackCH() throws PersistenceException {
191         try {
192             if (connection != null && !txManagedExternaly && transactional) {
193                 rollbackLocalTransaction();
194             }
195         } finally {
196             transactional = false;
197         }
198     }
199
200     public void releaseCHConnection() throws PersistenceException {
201         if (connection != null && txManagedExternaly) {
202             if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
203                 logger.log(BasicLevel.DEBUG, "Release the connection " + connection);
204             }
205             closeCHConnection();
206         }
207     }
208
209     public void closeCHConnection() throws PersistenceException {
210         if (connection != null) {
211             if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) {
212                 logger.log(BasicLevel.DEBUG, "Close the connection: " + connection);
213             }
214             try {
215                 mapper.closeConnection(connection);
216             } catch (PException e) {
217                 throw new PersistenceException(e);
218             } finally {
219                 connection = null;
220             }
221         }
222     }
223 }
224
Popular Tags