KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > DBCPDataSourceFactory


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DBCPDataSourceFactory.java,v 1.1 2003/02/17 07:16:14 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import javax.sql.DataSource JavaDoc;
14 import org.apache.commons.pool.ObjectPool;
15 import org.apache.commons.pool.impl.GenericObjectPool;
16 import org.apache.commons.dbcp.ConnectionFactory;
17 import org.apache.commons.dbcp.PoolingDataSource;
18 import org.apache.commons.dbcp.PoolableConnectionFactory;
19 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
20 import org.apache.log4j.Category;
21
22
23 /**
24  * Makes DataSources whose DB connections are pooled using Jakarta DBCP.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.1 $
28  */

29
30 public class DBCPDataSourceFactory implements PersistenceTestCase.DataSourceFactory
31 {
32     private static final Category LOG = Category.getInstance(PersistenceTestCase.class);
33
34     public DataSource JavaDoc makePooledDataSource(String JavaDoc dbDriver,
35                                            String JavaDoc dbURL,
36                                            String JavaDoc dbUser,
37                                            String JavaDoc dbPassword) throws Exception JavaDoc
38     {
39         Class.forName(dbDriver);
40
41         ObjectPool connectionPool = new GenericObjectPool(null);
42
43         ConnectionFactory connectionFactory =
44             new DriverManagerConnectionFactory(dbURL, dbUser, dbPassword);
45
46         PoolableConnectionFactory poolableConnectionFactory =
47             new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
48
49         DataSource JavaDoc ds = new PoolingDataSource(connectionPool);
50
51         LOG.info("Connection pool initialized: " + connectionPool);
52
53         return ds;
54     }
55 }
56
Popular Tags