KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > datasource > SingleConnectionDataSource


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.jdbc.datasource;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import javax.sql.DataSource JavaDoc;
30
31 import org.apache.commons.dbcp.ConnectionFactory;
32 import org.apache.commons.dbcp.PoolableConnectionFactory;
33 import org.apache.commons.dbcp.PoolingDataSource;
34 import org.apache.commons.pool.impl.GenericObjectPool;
35
36
37 public class SingleConnectionDataSource implements DataSource JavaDoc {
38
39     private PoolingDataSource dataSource;
40     private Connection JavaDoc singleConnection;
41
42     public SingleConnectionDataSource(Connection JavaDoc connection) throws SQLException JavaDoc {
43         singleConnection = connection;
44
45         GenericObjectPool connectionPool = new GenericObjectPool();
46         connectionPool.setMaxActive(1);
47         connectionPool.setMaxIdle(1);
48         connectionPool.setMinIdle(1);
49         connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
50
51         ConnectionFactory jdbcConnectionFactory = new ConnectionFactory() {
52
53             public Connection JavaDoc createConnection() throws SQLException JavaDoc {
54                 return singleConnection;
55             }
56
57         };
58
59         PoolableConnectionFactory connectionFactory =
60             new PoolableConnectionFactory(jdbcConnectionFactory,
61                                           connectionPool,
62                                           null,
63                                           null,
64                                           false,
65                                           true);
66         if (connectionFactory == null) {
67             throw new SQLException JavaDoc("Cannot create PoolableConnectionFactory");
68         }
69         dataSource = new PoolingDataSource(connectionPool);
70     }
71     
72     /**
73      * Create (if necessary) and return a connection to the database.
74      *
75      * @exception SQLException if a database access error occurs
76      */

77     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
78         return dataSource.getConnection();
79     }
80
81     /**
82      * Create (if necessary) and return a connection to the database.
83      *
84      * @param username Database user on whose behalf the Connection
85      * is being made
86      * @param password The database user's password
87      *
88      * @exception SQLException if a database access error occurs
89      */

90     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
91         return dataSource.getConnection(username, password);
92     }
93
94     /**
95      * Return the login timeout (in seconds) for connecting to the database.
96      *
97      * @exception SQLException if a database access error occurs
98      */

99     public int getLoginTimeout() throws SQLException JavaDoc {
100         return dataSource.getLoginTimeout();
101     }
102
103     /**
104      * Return the log writer being used by this data source.
105      *
106      * @exception SQLException if a database access error occurs
107      */

108     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
109         return dataSource.getLogWriter();
110     }
111
112     /**
113      * Set the login timeout (in seconds) for connecting to the database.
114      *
115      * @param loginTimeout The new login timeout, or zero for no timeout
116      *
117      * @exception SQLException if a database access error occurs
118      */

119     public void setLoginTimeout(int loginTimeout) throws SQLException JavaDoc {
120         dataSource.setLoginTimeout(loginTimeout);
121     }
122
123     /**
124      * Set the log writer being used by this data source.
125      *
126      * @param logWriter The new log writer
127      *
128      * @exception SQLException if a database access error occurs
129      */

130     public void setLogWriter(PrintWriter JavaDoc logWriter) throws SQLException JavaDoc {
131         dataSource.setLogWriter(logWriter);
132     }
133
134 }
135
Popular Tags