C3p0ConnectionService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of 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. /** Contibution AtosWorldline / Meteo France - mlux */
  35. package fr.paris.lutece.util.pool.service;

  36. import com.mchange.v2.c3p0.ComboPooledDataSource;
  37. import fr.paris.lutece.util.env.EnvUtil;

  38. import java.sql.Connection;
  39. import java.sql.SQLException;

  40. import java.util.Map;

  41. import javax.sql.DataSource;

  42. import org.apache.logging.log4j.LogManager;
  43. import org.apache.logging.log4j.Logger;

  44. /**
  45.  * C3P0 connection service
  46.  */
  47. public class C3p0ConnectionService implements ConnectionService
  48. {
  49.     /**
  50.      * C3P0 connection pool
  51.      */
  52.     private ComboPooledDataSource _dataSource;

  53.     /**
  54.      * Pool name
  55.      */
  56.     private String _strPoolName;

  57.     /**
  58.      * Log4j logger
  59.      */
  60.     private Logger _logger = LogManager.getLogger( this.getClass( ) );

  61.     /**
  62.      * {@inheritDoc }
  63.      */
  64.     @Override
  65.     public void init( Map<String, String> htParamsConnectionPool )
  66.     {
  67.         try
  68.         {
  69.             _dataSource = new ComboPooledDataSource( _strPoolName );

  70.             String strDriver = htParamsConnectionPool.get( getPoolName( ) + ".driver" );
  71.             _dataSource.setDriverClass( strDriver );

  72.             String strUrl = htParamsConnectionPool.get( getPoolName( ) + ".url" );
  73.             strUrl = EnvUtil.evaluate( strUrl, EnvUtil.PREFIX_ENV );
  74.             _dataSource.setJdbcUrl( strUrl );

  75.             String strUser = htParamsConnectionPool.get( getPoolName( ) + ".user" );
  76.             strUser = EnvUtil.evaluate( strUser, EnvUtil.PREFIX_ENV );
  77.             _dataSource.setUser( strUser );

  78.             String strPassword = htParamsConnectionPool.get( getPoolName( ) + ".password" );
  79.             strPassword = EnvUtil.evaluate( strPassword, EnvUtil.PREFIX_ENV );
  80.             _dataSource.setPassword( strPassword );

  81.             String strMaxConns = htParamsConnectionPool.get( getPoolName( ) + ".maxconns" );
  82.             int nMaxConns = ( strMaxConns == null ) ? 0 : Integer.parseInt( strMaxConns );
  83.             _dataSource.setMaxPoolSize( nMaxConns );

  84.             String strMinConns = htParamsConnectionPool.get( getPoolName( ) + ".initconns" );
  85.             int nInitConns = ( strMinConns == null ) ? 0 : Integer.parseInt( strMinConns );
  86.             _dataSource.setInitialPoolSize( nInitConns );
  87.             _dataSource.setMinPoolSize( nInitConns );
  88.         }
  89.         catch( Exception e )
  90.         {
  91.             _logger.error( "Error while initializing the pool {}", getPoolName( ), e );
  92.         }

  93.         _logger.info( "Initialization of the C3P0 pool named '{}', Min/Max pool size : {} / {}", ( ) -> getPoolName( ), _dataSource::getMinPoolSize,
  94.                 _dataSource::getMaxPoolSize );
  95.     }

  96.     /**
  97.      * {@inheritDoc }
  98.      */
  99.     @Override
  100.     public Connection getConnection( )
  101.     {
  102.         Connection conn = null;

  103.         try
  104.         {
  105.             if ( _dataSource != null )
  106.             {
  107.                 conn = _dataSource.getConnection( );

  108.                 if ( conn != null )
  109.                 {
  110.                     if ( _logger.isDebugEnabled( ) )
  111.                     {
  112.                         _logger.debug( "The connexion is get, Current/Max pool : {}/{}", _dataSource.getNumConnectionsAllUsers( ),
  113.                                 _dataSource.getMaxPoolSize( ) );
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         catch( Exception e )
  119.         {
  120.             _logger.error( "Erreur when getting the connexion with the pool : {}", getPoolName( ), e );
  121.         }

  122.         return conn;
  123.     }

  124.     /**
  125.      * {@inheritDoc }
  126.      */
  127.     @Override
  128.     public void freeConnection( Connection conn )
  129.     {
  130.         try
  131.         {
  132.             conn.close( );

  133.             if ( _logger.isDebugEnabled( ) )
  134.             {

  135.                 _logger.debug( "The connexion is released, Current/Max pool : {}/{}", _dataSource.getNumConnectionsAllUsers( ), _dataSource.getMaxPoolSize( ) );

  136.             }
  137.         }
  138.         catch( SQLException e )
  139.         {
  140.             _logger.error( "SQL error when releasing the connexion with the pool : {}", getPoolName( ), e );
  141.         }
  142.         catch( Exception e )
  143.         {
  144.             _logger.error( "Error while releasing the connexion with the pool : {}", getPoolName( ), e );
  145.         }
  146.     }

  147.     /**
  148.      * {@inheritDoc }
  149.      */
  150.     @Override
  151.     public void setPoolName( String poolName )
  152.     {
  153.         this._strPoolName = poolName;
  154.     }

  155.     /**
  156.      * {@inheritDoc }
  157.      */
  158.     @Override
  159.     public String getPoolName( )
  160.     {
  161.         return _strPoolName;
  162.     }

  163.     /**
  164.      * {@inheritDoc }
  165.      */
  166.     @Override
  167.     public void setLogger( Logger log )
  168.     {
  169.         this._logger = log;
  170.     }

  171.     /**
  172.      * {@inheritDoc }
  173.      */
  174.     @Override
  175.     public Logger getLogger( )
  176.     {
  177.         return _logger;
  178.     }

  179.     /**
  180.      * {@inheritDoc }
  181.      */
  182.     @Override
  183.     public void release( )
  184.     {
  185.         _dataSource.close( );
  186.     }

  187.     /**
  188.      * {@inheritDoc }
  189.      */
  190.     @Override
  191.     public int getCurrentConnections( )
  192.     {
  193.         int nCurrentConnections = -1;

  194.         try
  195.         {
  196.             nCurrentConnections = _dataSource.getNumConnections( );
  197.         }
  198.         catch( SQLException ex )
  199.         {
  200.             _logger.error( "GetCurrentConnections error : {}", ex.getMessage( ), ex );
  201.         }

  202.         return nCurrentConnections;
  203.     }

  204.     /**
  205.      * {@inheritDoc }
  206.      */
  207.     @Override
  208.     public int getMaxConnections( )
  209.     {
  210.         return _dataSource.getMaxPoolSize( );
  211.     }

  212.     /**
  213.      * {@inheritDoc }
  214.      */
  215.     @Override
  216.     public String getPoolProvider( )
  217.     {
  218.         return "C3P0";
  219.     }

  220.     /**
  221.      * {@inheritDoc }
  222.      */
  223.     @Override
  224.     public DataSource getDataSource( )
  225.     {
  226.         return _dataSource;
  227.     }
  228. }