Can't open connection - java

I develop an applivation with very load(request).
I used following technologies in my appliation:
Jpa/Hibernate as persistense layer
Spring and Spring Dao
C3p0 as connection pooling
my problem is : I run my application , when number of request increase, throw exception in
persistense layer that"Cannt open connection"
I increase oracle max session but my problem not solve
I indept in C3p0 document and test its options but my problem not solve.
Thank you for your attention

You increased max sessions on Oracle, but you didn't increase the max size of your connection pool. The exception is telling you that your pool is exhausted. Either find what's holding connections open and get them released sooner, or increase the number of max active connections in the pool.

Is it possible for you to post the Spring configuration for your DataSource. I would expect something like:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="${jdbc.connection.url}"/>
<property name="user" value="${jdbc.connection.username}"/>
<property name="password" value="${jdbc.connection.password}"/>
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="100"/>
</bean>
With another bean configured where the dataSource is passed by reference:
<bean id="mySampleDao" class="com.example.dao.MySampleDao">
<property name="dataSource" ref="dataSource" />
</bean>
Is this what you have?
What version of Oracle are you using?

Related

Spring Throws "ERROR SqlExceptionHelper: Already closed" after timeout

I have a spring application hosted on to the server (Tomcat 8.5). It goes idle if no one uses it. I already knew that timeout will occur if the DB is in idle state for 8 hours (Default timeout of MySQL). As mentioned in Spring Autoreconnect and Connection lost overnight post i have tried the solution available here.I have tried configuring application.properties but that doesn't bring any solution to the problem.
(PS:I'm not changing anything other than application.properties in my Spring Application).
Well if this
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1
or this
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
didnt work maybe try this
Post SpringBoot 1.4 names have changed
They have defined new specific namespaces for the four connections pools spring supports: tomcat, hikari, dbcp, dbcp2.
spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.validationQuery=SELECT 1
If problem doesn't solve even after including properties as in application.properties, Then problem will be solved when including testOnBorrow,validationQuery in application-context.xml located in src/main/resources
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver.classname}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="initialSize" value="2"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="-1"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="600"/>
<property name="logAbandoned" value="true"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
</bean>
The solution is to validate connection thread when it is borrowed from thread pool by enabling testOnBorrow and providing validationQuery.

How to configure JavaMelody to Monitor Jdbc Connections in C3p0 DataSource

I'm using Spring configuration file to configure C3P0. To monitor DataSource I configured net.bull.javamelody.SpringDataSourceFactoryBean as mentioned in the user guide of javamelody. But my report is showing 0 Active jdbc connections where as my minPoolSize is 10. What did I miss?
In web.xml added monitoring-spring.xml
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath:net/bull/javamelody/monitoring-spring.xml,
</param-value>
</context-param>
In Spring jdbc Configuration file is:
<bean id="sql2oDatasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="#{dbProps['ops.jdbc.driverClassName']}"/>
<property name="jdbcUrl" value="#{dbProps['ops.jdbc.url']}"/>
<property name="user" value="#{dbProps['ops.jdbc.username']}"/>
<property name="password" value="#{dbProps['ops.jdbc.password']}"/>
<property name="maxPoolSize" value="#{dbProps['ops.c3p0.max_size']}"/>
<property name="minPoolSize" value="#{dbProps['ops.c3p0.min_size']}"/>
<property name="maxStatements" value="#{dbProps['ops.c3p0.max_statements']}"/>
<property name="checkoutTimeout" value="#{dbProps['ops.c3p0.timeout']}"/>
<property name="preferredTestQuery" value="SELECT 1"/>
</bean>
<!-- Configuring the session factory for SQL-2-O -->
<bean id="sql2oSession" class="org.sql2o.Sql2o">
<constructor-arg ref="wrappedDBDataSource"/>
<constructor-arg value="PostgreSQL" type="org.sql2o.QuirksMode"/>
</bean>
<bean id="wrappedDBDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean" primary="true">
<property name="targetName" value="sql2oDatasource"/>
</bean>
I tried to pass DriverClass as net.bull.javamelody.JdbcDriver in datasource and driver as:
<property name="properties">
<props>
<prop key="driver">org.postgresql.Driver</prop>
</props>
</property>
But postgresql driver is not getting registered this way.
Your configuration looks ok according to documentation. You see db active connection, pool size values on report that also means your config is ok.
In the other hand active db connection count means how many connections from db connection pool is connection operating with db at that moment. 0 active connections is good for you(if your application is working properly). It means your db operations are quickly done. It is hard to catch active connection while db operations are fast.

Multi connection pool configurations for different modules in application

Our application has multiple modules, each module use its own schema in the same mysql database. Now I need to make different connection pool configurations for each module because of their different db resource consuming nature, i.e. some module may have 20 active connections at a point of time, but others may just have 1 max. I have searched here and other forums, couldn't find a solution, just this topic is not about multi-tenancy or multi-database, all schemas are in the same db.
Here's the config we have:
<bean id="dataSource" class="our.own.package.RoutingDataSource"> <!-- RoutingDataSource extends spring AbstractDataSource -->
<property name="master" ref="masterDS"/>
</bean>
<bean id="abstractDataSource" abstract="true">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="initialPoolSize" value="#initial.pool.size#" />
<property name="minPoolSize" value="#min.pool.size#" />
<property name="maxPoolSize" value="#max.pool.size#" /> <!-- I want to have different configs for each module in our application -->
</bean>
<bean id="masterDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" parent="abstractDataSource">
<property name="jdbcUrl" value="jdbc:mysql://#host#/" />
<property name="user" value="#user#" />
<property name="password" value="#pwd#" />
<property name="dataSourceName" value="#dbName#" />
</bean>
So now I have two questions:
1) Is it possible to have different connection pool configurations for one datasource in Spring?
2) If I have to go with the multiple datasource way(one datasource for one module), is implementing Spring's AbstractRoutingDataSource the correct way to go?
Thank you!
Ad.1 Your data source is in the fact connection pool so you want to have multiple pools on the top of another pool. You can do it but you will face many other problems.
Ad.2. Yes definitely. You already have RoutingDataSource so this one should be implementation of AbstractRoutingDataSource. Probably you already have logic there to determine current the data source routing key which should be used to do the lookup.

tomcat jdbc pool max active not working?

I use spring jdbc template for app.. and deploy it in tomcat.. I want to use connection pool with tomcat jdbc. My connection configuration is
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3310/mydb" />
<property name="username" value="***" />
<property name="password" value="***" />
<property name="maxWait" value="10000" />
<property name="removeAbandonedTimeout" value="60" />
<property name="removeAbandoned" value="true" />
<property name="logAbandoned" value="false" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="minIdle" value="10" />
</bean>
I don't know how, but when I run some test, and check the max thread in mysql, it show that the active thread is more than the maxActive configured in the configuration. So, why the maxActive in the configuration not working? And how to make it work? For example, the maxActive is 100 but when I check in mysql, the active thread is more than the maxActive.
maxActive (int) The maximum number of active connections that can be
allocated from this pool at the same time. The default value is 100
maxIdle (int) The maximum number of connections that should be kept in
the pool at all times. Default value is maxActive:100 Idle connections
are checked periodically (if enabled) and connections that been idle
for longer than minEvictableIdleTimeMillis will be released. (also see
testWhileIdle)
So i should recommend you to use maxIdle too, example:
<property name="maxIdle" value="100">
But maybe there is a problem, if you could show the code of your connection management, it would be helpful.
Here is an interesting link of a connection problem with declarative and programmatic transaction management with Spring: Connection pool problem with Spring and programmatic transaction management
I got the same problem with you and my tomcat version is tomcat 9.
Here is my solution: you should set maxTotal value 100 instead of maxActive.
maxTotal: Maximum number of database connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to -1 for no limit.
FROM http://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html.

Hibernate and mysql timeout issue

I have been having trouble with Hibernate and Mysql timeout error.I am also using properties of c3p0(connection provider). After my Hibernate/MySQL have been running after 8 hours(which is default timeout value in Mysql), I have the exception. But it doesn't help.
property for auto reconnect also not working.
Here is my Hibernate Configuration:
<property name="connection_provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
**<property name="connection.autoReconnect"> true</property>
<property name="connection.autoReconnectForPools">true</property>**
<property name="connection.is-connection-validation-required">true</property>
<property name="c3p0.validate">true</property>
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_second_level_cache">false</property>
<property name="c3p0.idle_test_period">20</property>
<property name="c3p0.timeout">40</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.acquireRetryAttempts">10</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.maxIdleTime">300</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.minPoolSize">10</property>
<property name="c3p0.preferredTestQuery">select 1;</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/doqeap</property>
<property name="connection.user">root</property>
<property name="connection.password">*******</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.jdbc.batch_size">0</property>
<mapping></mapping>
please help me to sort out this problem.
Thanks
If the Connection timeout is the issue, then Connection testing should eliminate, wither via tests on checkout (reliable but imposes a client visible performance cost) or tests on checking + idle tests.
Looking at you config params, it looks like you mean to set tests on checkouts and idle tests. I'd expect that c3p0 would eliminate timed out Exceptions before your app saw them. If that hasn't happened, it'd be interesting to see two things: 1) c3p0's config, which gets logged at INFO when the pool is initialized -- is c3p0, through the hibernate layer, seeing the configuration you intend? 2) the Exception that your app receives when it encounters the stale Connections.
Good luck!

Categories