KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > conprovider > SinglePerThreadConnectionProvider


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/conprovider/SinglePerThreadConnectionProvider.java,v 1.5 2004/08/18 12:25:57 hkollmann Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/18 12:25:57 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library 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. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.conprovider;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.util.Properties JavaDoc;
31
32
33
34 /**
35  * Single Connection per thread provider. <br> provides one connection for all
36  *
37  * @author Henner Kollmann
38  */

39 public class SinglePerThreadConnectionProvider extends ConnectionProvider {
40    private static final ThreadLocal JavaDoc singlePerThread = new ThreadLocal JavaDoc();
41
42    /**
43     * Default constructor.
44     *
45     * @exception Exception Description of the Exception
46     * @throws Exception because of the <code>throws Exception</code> clause of
47     * the <code>init</code> method.
48     */

49    public SinglePerThreadConnectionProvider() throws Exception JavaDoc {
50       super();
51    }
52
53    /**
54     * Get a JDBC Connection
55     *
56     * @return a JDBC Connection
57     *
58     * @exception SQLException Description of the Exception
59     */

60    protected synchronized Connection JavaDoc getConnection() throws SQLException JavaDoc {
61       Connection JavaDoc con = (Connection JavaDoc) singlePerThread.get();
62
63       if (con == null) {
64          Properties JavaDoc props = getPrefs()
65                                .getProperties();
66
67          // uses custom jdbc properties;
68
if ((props != null) && !props.isEmpty()) {
69             props.put("user", getPrefs().getUser());
70             props.put("password", getPrefs().getPassword());
71             con = DriverManager.getConnection(getPrefs().getJdbcURL(), props);
72          }
73          // "plain" flavour;
74
else {
75             con = DriverManager.getConnection(getPrefs().getJdbcURL(),
76                                               getPrefs().getUser(),
77                                               getPrefs().getPassword());
78          }
79
80          singlePerThread.set(con);
81       }
82
83       return new SingleConnectionWrapper(con);
84    }
85
86
87    /**
88     * Initialize the ConnectionProvider.
89     *
90     * @throws Exception if any error occurs
91     */

92    protected void init() throws Exception JavaDoc {
93       Class.forName(getPrefs().getJdbcDriver())
94            .newInstance();
95    }
96 }
97
Popular Tags