View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  
35  /** Contibution AtosWorldline / Meteo France - mlux */
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   * C3P0 connection service
52   */
53  public class C3p0ConnectionService implements ConnectionService
54  {
55      /**
56       * C3P0 connection pool
57       */
58      private ComboPooledDataSource _dataSource;
59  
60      /**
61       * Pool name
62       */
63      private String _strPoolName;
64  
65      /**
66       * Log4j logger
67       */
68      private Logger _logger = Logger.getLogger( this.getClass(  ) );
69  
70      /**
71       * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
161      */
162     public void setPoolName( String poolName )
163     {
164         this._strPoolName = poolName;
165     }
166 
167     /**
168      * {@inheritDoc }
169      */
170     public String getPoolName(  )
171     {
172         return _strPoolName;
173     }
174 
175     /**
176      * {@inheritDoc }
177      */
178     public void setLogger( Logger log )
179     {
180         this._logger = log;
181     }
182 
183     /**
184      * {@inheritDoc }
185      */
186     public Logger getLogger(  )
187     {
188         return _logger;
189     }
190 
191     /**
192      * {@inheritDoc }
193      */
194     public void release(  )
195     {
196     }
197 
198     /**
199      * {@inheritDoc }
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      * {@inheritDoc }
219      */
220     public int getMaxConnections(  )
221     {
222         return _dataSource.getMaxPoolSize(  );
223     }
224 
225     /**
226      * {@inheritDoc }
227      */
228     public String getPoolProvider(  )
229     {
230         return "C3P0";
231     }
232 
233     /**
234      * {@inheritDoc }
235      */
236     public DataSource getDataSource(  )
237     {
238         return _dataSource;
239     }
240 }