1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package fr.paris.lutece.util.pool.service;
37
38 import com.mchange.v2.c3p0.ComboPooledDataSource;
39
40 import org.apache.log4j.Logger;
41
42 import java.sql.Connection;
43 import java.sql.SQLException;
44
45 import java.util.Hashtable;
46
47 import javax.sql.DataSource;
48
49
50
51
52
53 public class C3p0ConnectionService implements ConnectionService
54 {
55
56
57
58 private ComboPooledDataSource _dataSource;
59
60
61
62
63 private String _strPoolName;
64
65
66
67
68 private Logger _logger = Logger.getLogger( this.getClass( ) );
69
70
71
72
73 public void init( Hashtable<String, String> htParamsConnectionPool )
74 {
75 try
76 {
77 _dataSource = new ComboPooledDataSource( _strPoolName );
78
79 String strDriver = htParamsConnectionPool.get( getPoolName( ) + ".driver" );
80 _dataSource.setDriverClass( strDriver );
81
82 String strUrl = htParamsConnectionPool.get( getPoolName( ) + ".url" );
83 _dataSource.setJdbcUrl( strUrl );
84
85 String strUser = htParamsConnectionPool.get( getPoolName( ) + ".user" );
86 _dataSource.setUser( strUser );
87
88 String strPassword = htParamsConnectionPool.get( getPoolName( ) + ".password" );
89 _dataSource.setPassword( strPassword );
90
91 String strMaxConns = htParamsConnectionPool.get( getPoolName( ) + ".maxconns" );
92 int nMaxConns = ( strMaxConns == null ) ? 0 : Integer.parseInt( strMaxConns );
93 _dataSource.setMaxPoolSize( nMaxConns );
94
95 String strMinConns = htParamsConnectionPool.get( getPoolName( ) + ".initconns" );
96 int nInitConns = ( strMinConns == null ) ? 0 : Integer.parseInt( strMinConns );
97 _dataSource.setInitialPoolSize( nInitConns );
98 _dataSource.setMinPoolSize( nInitConns );
99 }
100 catch ( Exception e )
101 {
102 _logger.error( "Error while initializing the pool " + getPoolName( ), e );
103 }
104
105 _logger.info( "Initialization of the C3P0 pool named '" + getPoolName( ) + "', Min/Max pool size : " +
106 _dataSource.getMinPoolSize( ) + "/" + _dataSource.getMaxPoolSize( ) );
107 }
108
109
110
111
112 public Connection getConnection( )
113 {
114 Connection conn = null;
115
116 try
117 {
118 if ( _dataSource != null )
119 {
120 conn = _dataSource.getConnection( );
121
122 if ( conn != null )
123 {
124 _logger.debug( "The connexion is get, Current/Max pool : " +
125 _dataSource.getNumConnectionsAllUsers( ) + "/" + _dataSource.getMaxPoolSize( ) );
126 }
127 }
128 }
129 catch ( Exception e )
130 {
131 _logger.error( "Erreur when getting the connexion with the pool : " + getPoolName( ), e );
132 }
133
134 return conn;
135 }
136
137
138
139
140 public void freeConnection( Connection conn )
141 {
142 try
143 {
144 conn.close( );
145
146 _logger.debug( "The connexion is released, Current/Max pool : " +
147 _dataSource.getNumConnectionsAllUsers( ) + "/" + _dataSource.getMaxPoolSize( ) );
148 }
149 catch ( SQLException e )
150 {
151 _logger.error( "SQL error when releasing the connexion with the pool : " + getPoolName( ), e );
152 }
153 catch ( Exception e )
154 {
155 _logger.error( "Error while releasing the connexion with the pool : " + getPoolName( ), e );
156 }
157 }
158
159
160
161
162 public void setPoolName( String poolName )
163 {
164 this._strPoolName = poolName;
165 }
166
167
168
169
170 public String getPoolName( )
171 {
172 return _strPoolName;
173 }
174
175
176
177
178 public void setLogger( Logger log )
179 {
180 this._logger = log;
181 }
182
183
184
185
186 public Logger getLogger( )
187 {
188 return _logger;
189 }
190
191
192
193
194 public void release( )
195 {
196 }
197
198
199
200
201 public int getCurrentConnections( )
202 {
203 int nCurrentConnections = -1;
204
205 try
206 {
207 nCurrentConnections = _dataSource.getNumConnections( );
208 }
209 catch ( SQLException ex )
210 {
211 _logger.error( "GetCurrentConnections error : " + ex.getMessage( ), ex );
212 }
213
214 return nCurrentConnections;
215 }
216
217
218
219
220 public int getMaxConnections( )
221 {
222 return _dataSource.getMaxPoolSize( );
223 }
224
225
226
227
228 public String getPoolProvider( )
229 {
230 return "C3P0";
231 }
232
233
234
235
236 public DataSource getDataSource( )
237 {
238 return _dataSource;
239 }
240 }