Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4.1.9 c3p0 settings, and way too many connections to database

I'm using Hibernate 4.1.9 in a Java web application (that uses a Oracle 11g database) and seem to be getting some run-away connections even though i'm using c3p0 pooling.

it seems this should be managed in my config file with the appropriate properties, but I am struggling to get them set up correctly.

here is my persistence.xml file with the properties settings in it:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="RPRM_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

  <property name="hibernate.connection.username" value="username"/>
  <property name="hibernate.connection.password" value="********"/>        

  <property name="hibernate.connection.url" value="jdbc:oracle:thin:@xxxxx.xxxx.com:1771:xxxxxx"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
  <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>

  <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /> <!-- hibernate 4.1.9 -->
  <property name="hibernate.c3p0.acquireIncrement" value="3"/>
  <property name="hibernate.c3p0.maxIdleTime" value="3600"/>
  <property name="hibernate.c3p0.minPoolSize" value="6"/>
  <property name="hibernate.c3p0.maxPoolSize" value="20"/>
  <property name="hibernate.c3p0.maxStatements" value="20"/>
  <property name="hibernate.c3p0.idleConnectionTestPeriod" value="1800"/> <!-- seconds -->
  <property name="hibernate.c3p0.maxConnectionAge" value="100"/>
  <property name="hibernate.c3p0.maxIdleTimeExcessConnections" value="300"/>
  <property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>
  <property name="hibernate.c3p0.preferredTestQuery" value="select 1 from dual"/>

  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

  <property name="hibernate.show_sql" value="false"/>
  <property name="hibernate.format_sql" value="false" />

</properties>

and when i start up the app i see information that Hibernate is using the c3p0 settings:

Mar 7, 2013 11:15:21 AM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
Mar 7, 2013 11:15:21 AM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@330d4ac9 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@87961d4a [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 30huhj8tjhzyr1ovdu4t|6196fc, idleConnectionTestPeriod -> 1800, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 100, maxIdleTime -> 3600, maxIdleTimeExcessConnections -> 300, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 0, minPoolSize -> 6, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@8d40ef6e [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 30huhj8tjhzyr1ovdu4t|1e9c3f, jdbcUrl -> jdbc:oracle:thin:@xxxxx.xxxxx.com:1771:xxxxx, properties -> {user=******, password=******, autocommit=true, release_mode=auto} ], preferredTestQuery -> select 1 from dual, propertyCycle -> 0, testConnectionOnCheckin -> true, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 30huhj8tjhzyr1ovdu4t|fa0094, numHelperThreads -> 3 ]
Mar 7, 2013 11:15:24 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Mar 7, 2013 11:15:24 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Mar 7, 2013 11:15:24 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory

unfortunately approximately every 2 minutes Oracle is registering a handful (usually 12 at a time) of new connections (even when no users are accessing the application)

I apologize if i am missing something obvious in my settings - i've scoured thru documentation, and the web trying to understand all the properties, but may be missing something or have something set wrong.

note, i am using hibernate 4.1.9.Final, and Oracle 11g in a Tomcat 6.0.14 container.

thank you for your time!

like image 871
russellelbert Avatar asked Mar 07 '13 19:03

russellelbert


1 Answers

You have set maxConnectionAge to 100 seconds. This means that if a connection is older than 100 seconds it will be forcefully closed, this also means that if your pool is idle, it will create 6 new connections every 100 seconds.

The documentation says:

Seconds, effectively a time to live. A Connection older than maxConnectionAge will be destroyed and purged from the pool. This differs from maxIdleTime in that it refers to absolute age. Even a Connection which has not been much idle will be purged from the pool if it exceeds maxConnectionAge. Zero means no maximum absolute age is enforced.

Either set maxConnectionAge to 0 to disable this or set it to a much higher number.

like image 114
Mark Rotteveel Avatar answered Oct 31 '22 14:10

Mark Rotteveel