KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jdbc > ConnectionContext


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jdbc;
31
32 import com.caucho.util.L10N;
33 import com.caucho.util.Log;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39 import java.sql.Connection JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Abstract way of grabbing data from the JDBC connection.
47  */

48 public class ConnectionContext {
49   private static final L10N L = new L10N(ConnectionContext.class);
50   private static final Logger JavaDoc log = Log.open(ConnectionContext.class);
51
52   private static InitialContext JavaDoc _initialContext;
53
54   private static ThreadLocal JavaDoc<HashMap JavaDoc<String JavaDoc,ConnectionContext>> _localConn
55     = new ThreadLocal JavaDoc<HashMap JavaDoc<String JavaDoc,ConnectionContext>>();
56
57   private int _depth;
58   private Connection JavaDoc _conn;
59
60   public static void begin(String JavaDoc jndiName)
61   {
62     HashMap JavaDoc<String JavaDoc,ConnectionContext> map = _localConn.get();
63
64     if (map == null) {
65       map = new HashMap JavaDoc<String JavaDoc,ConnectionContext>(8);
66
67       _localConn.set(map);
68     }
69
70     ConnectionContext cxt = map.get(jndiName);
71
72     if (cxt == null) {
73       cxt = new ConnectionContext();
74       
75       map.put(jndiName, cxt);
76     }
77
78     cxt._depth++;
79   }
80
81   public static Connection JavaDoc getConnection(String JavaDoc jndiName)
82     throws SQLException JavaDoc
83   {
84     HashMap JavaDoc<String JavaDoc,ConnectionContext> map = _localConn.get();
85
86     if (map == null)
87       throw new IllegalStateException JavaDoc(L.l("'{0}' is not an available connection.",
88                       jndiName));
89
90     ConnectionContext cxt = map.get(jndiName);
91
92     if (cxt == null || cxt._depth == 0)
93       throw new IllegalStateException JavaDoc(L.l("'{0}' is not an available connection.",
94                       jndiName));
95
96     if (cxt._conn == null) {
97       try {
98     DataSource JavaDoc ds = (DataSource JavaDoc) _initialContext.lookup(jndiName);
99
100     cxt._conn = ds.getConnection();
101       } catch (NamingException JavaDoc e) {
102     throw new IllegalStateException JavaDoc(e);
103       }
104     }
105
106     return null;
107   }
108
109   public static void end(String JavaDoc jdbcName)
110   {
111     HashMap JavaDoc<String JavaDoc,ConnectionContext> map = _localConn.get();
112
113     if (map == null)
114       return;
115
116     ConnectionContext cxt = map.get(jdbcName);
117
118     if (cxt == null)
119       return;
120
121     if (--cxt._depth == 0) {
122       Connection JavaDoc conn = cxt._conn;
123
124       try {
125     cxt._conn = null;
126     conn.close();
127       } catch (SQLException JavaDoc e) {
128     log.log(Level.FINE, e.toString(), e);
129       }
130     }
131   }
132
133   private static Context JavaDoc getInitialContext()
134   {
135     if (_initialContext == null) {
136       try {
137     _initialContext = new InitialContext JavaDoc();
138       } catch (NamingException JavaDoc e) {
139       }
140     }
141
142     return _initialContext;
143   }
144 }
145
Popular Tags