1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import com.mchange.v2.c3p0.DataSources; |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| |
13 |
| import javax.sql.DataSource; |
14 |
| import java.sql.Connection; |
15 |
| import java.sql.SQLException; |
16 |
| import java.util.Enumeration; |
17 |
| import java.util.Properties; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class C3p0ConnectionFactory extends NonManagedConnectionFactory |
25 |
| { |
26 |
| private static final Log log = LogFactory.getLog(C3p0ConnectionFactory.class); |
27 |
| |
28 |
| private DataSource ds; |
29 |
| |
30 |
65
| public void setConfig(AdjListJDBCCacheLoaderConfig config)
|
31 |
| { |
32 |
65
| super.setConfig(config);
|
33 |
| |
34 |
65
| Properties properties = config.getProperties();
|
35 |
65
| Enumeration e = properties.propertyNames();
|
36 |
65
| while (e.hasMoreElements())
|
37 |
| { |
38 |
471
| String property = (String) e.nextElement();
|
39 |
471
| if (property.startsWith("c3p0."))
|
40 |
| { |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
2
| String xmlPropertyValue = properties.getProperty(property);
|
52 |
2
| String sysPropertyValue = System.getProperty(property);
|
53 |
2
| if (System.getProperty(property) == null)
|
54 |
| { |
55 |
1
| System.setProperty(property, xmlPropertyValue);
|
56 |
1
| if (log.isDebugEnabled())
|
57 |
| { |
58 |
0
| log.debug("c3p0 property defined in XML: " + property + "=" + xmlPropertyValue);
|
59 |
| } |
60 |
| } |
61 |
| else |
62 |
| { |
63 |
1
| if (log.isDebugEnabled())
|
64 |
| { |
65 |
0
| log.debug(property + "=" + sysPropertyValue + " defined as system property. It will override the value defined in XML which was: " + xmlPropertyValue);
|
66 |
| }; |
67 |
| } |
68 |
| } |
69 |
| } |
70 |
| } |
71 |
| |
72 |
65
| public void start() throws Exception
|
73 |
| { |
74 |
| |
75 |
65
| super.start();
|
76 |
| |
77 |
65
| DataSource unpooled = DataSources.unpooledDataSource(getUrl(), getUsr(), getPwd());
|
78 |
65
| ds = DataSources.pooledDataSource(unpooled);
|
79 |
| |
80 |
65
| if (log.isDebugEnabled())
|
81 |
| { |
82 |
0
| log.debug("Pooled datasource(url=" + getUrl() + ",usr=" + getUsr() + ",pwd=" + getPwd() + ") started.");
|
83 |
| } |
84 |
| } |
85 |
| |
86 |
19957
| public Connection checkoutConnection() throws SQLException
|
87 |
| { |
88 |
19957
| Connection connection = ds.getConnection();
|
89 |
0
| if (log.isTraceEnabled()) { log.trace("Connection checked out: " + connection); }
|
90 |
19956
| return connection;
|
91 |
| } |
92 |
| |
93 |
65
| public void stop()
|
94 |
| { |
95 |
65
| try
|
96 |
| { |
97 |
65
| DataSources.destroy(ds);
|
98 |
0
| if (log.isDebugEnabled()) { log.debug("Pooled datasource destroyed."); }
|
99 |
| } catch (SQLException sqle) |
100 |
| { |
101 |
0
| log.warn("Could not destroy C3P0 connection pool: " + ds, sqle);
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
1
| protected DataSource getDataSource()
|
106 |
| { |
107 |
1
| return ds;
|
108 |
| } |
109 |
| } |