KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > simpletrans > TransDataSource


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.simpletrans;
56
57 import java.io.PrintWriter JavaDoc;
58 import java.sql.Connection JavaDoc;
59 import java.sql.SQLException JavaDoc;
60 import java.util.Map JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import javax.sql.DataSource JavaDoc;
63 import javax.naming.Reference JavaDoc;
64 import javax.naming.Referenceable JavaDoc;
65 import javax.naming.StringRefAddr JavaDoc;
66
67 import org.apache.log4j.Logger;
68
69 import org.lateralnz.common.util.Constants;
70 import org.lateralnz.common.util.JNDIUtils;
71 import org.lateralnz.common.util.StackThreadLocal;
72
73 /**
74  * a transaction aware data source, wrapping a 'real' datasource (ensuring that
75  * all connections returned are wrapped by a TransConnection)
76  *
77  * @author J R Briggs
78  */

79 public class TransDataSource implements Constants, DataSource JavaDoc, Referenceable JavaDoc {
80   private static final Logger log = Logger.getLogger(TransDataSource.class.getName());
81   private static Map JavaDoc dataSources = new HashMap JavaDoc();
82   private static SimpleTransactionManager stm = null;
83   
84   private String JavaDoc name;
85   private DataSource JavaDoc source;
86   
87   public TransDataSource(String JavaDoc name, DataSource JavaDoc source) throws Exception JavaDoc {
88     this.name = name;
89     this.source = source;
90     dataSources.put(name, this);
91     
92     if (stm == null) {
93       synchronized (TransDataSource.class) {
94         if (stm == null) {
95           try {
96             Object JavaDoc tmp = JNDIUtils.get("java:/TransactionManager");
97             if (tmp instanceof SimpleTransactionManager) {
98               stm = (SimpleTransactionManager)tmp;
99             }
100             else {
101               stm = (SimpleTransactionManager)tmp;
102             }
103           }
104           catch (Exception JavaDoc e) {
105             log.error(e);
106             throw e;
107           }
108         }
109       }
110     }
111   }
112   
113  /**
114   * helper method for the object factory associated with this class
115   */

116   protected static final TransDataSource getTransDataSource(String JavaDoc name) {
117     return (TransDataSource)dataSources.get(name);
118   }
119
120  /**
121   * get a connection (see the next getConnection method for more detail)
122   */

123   public Connection JavaDoc getConnection() throws SQLException JavaDoc {
124     return getConnection(null, null);
125   }
126   
127  /**
128   * get a jdbc connection -- if the calling thread is in a transaction, then
129   * this connection will be wrapped by a TransConnection object, otherwise
130   * the base connection is returned
131   *
132   * NOTE: RequiresNew is -NOT- handled properly at the moment
133   */

134   public Connection JavaDoc getConnection(String JavaDoc str, String JavaDoc str1) throws SQLException JavaDoc {
135     String JavaDoc methodTrans = (String JavaDoc)StackThreadLocal.peek();
136
137     if (stm.isInTransaction()
138        && (methodTrans != null && (methodTrans.equals(REQUIRED)
139            || methodTrans.equals(SUPPORTS)
140            || methodTrans.equals(REQUIRES_NEW)))) {
141       SimpleTransaction trans = (SimpleTransaction)stm.getTransaction();
142       Object JavaDoc tmp = trans.getResource(name);
143       TransConnection tc;
144       if (tmp != null) {
145         tc = (TransConnection)tmp;
146         return tc;
147       }
148       else if (methodTrans.equals(REQUIRED) || methodTrans.equals(REQUIRES_NEW)) {
149         tc = new TransConnection(name, getConn(str, str1), true);
150         trans.enlistResource(tc);
151         return tc;
152       }
153     }
154
155     Connection JavaDoc conn = getConn(str, str1);
156     conn.setAutoCommit(true);
157     return conn;
158   }
159   
160  /**
161   * call getConnection on the underlying datasource
162   */

163   private final Connection JavaDoc getConn(String JavaDoc str, String JavaDoc str1) throws SQLException JavaDoc {
164     long time = System.currentTimeMillis();
165     try {
166     if (str == null && str1 == null) {
167       return source.getConnection();
168     }
169     else {
170       return source.getConnection(str, str1);
171     }
172     }
173     catch (SQLException JavaDoc e) {
174       time = System.currentTimeMillis() - time;
175       if (log.isDebugEnabled()) {
176         log.debug("time taken at SQL error point was " + time + "ms");
177       }
178       e.printStackTrace();
179       throw e;
180     }
181   }
182   
183  /**
184   * @see javax.sql.DataSource#setLogWriter
185   */

186   public void setLogWriter(PrintWriter JavaDoc pw) throws SQLException JavaDoc {
187     source.setLogWriter(pw);
188   }
189
190  /**
191   * @see javax.sql.DataSource#getLogWriter
192   */

193   public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
194     return source.getLogWriter();
195   }
196   
197  /**
198   * @see javax.sql.DataSource#setLoginTimeout
199   */

200   public void setLoginTimeout(int timeout) throws SQLException JavaDoc {
201     source.setLoginTimeout(timeout);
202   }
203   
204  /**
205   * @see javax.sql.DataSource#getLoginTimeout
206   */

207   public int getLoginTimeout() throws SQLException JavaDoc {
208     return source.getLoginTimeout();
209   }
210   
211  /**
212   * reference method required so this object can be loaded into a registry
213   */

214     public Reference JavaDoc getReference() {
215     Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), TransDataSourceObjectFactory.class.getName(), null);
216     ref.add(new StringRefAddr JavaDoc("name", name));
217     return ref;
218   }
219 }
Popular Tags