JDBC connection to Microsoft SQL Server doesn't reconnect - java

I have a database that is connected through an unreliable network connection to an application server, so occasionally the connections break down. Each time this happens, all database connections in the AP pool will need to reconnect - which they don't unfortunately.
I went through different setups of c3p0, dbcp and bonecp as pools and used JTDS as well as the SQL Server V3 driver, (I even ditched pooling atogether to try out whether the regular DataSource would be able to create a new unpooled connection - which it didn't) but all configuration variants don't seem able to recover after network failure.
To clarify, none of the DataSources were able to get a new Connection. Is there some inherent Problem with the MS Sql Server regarding reconnects? Am I missing something fundamental here?
I realize this might not be of big help, but just as an example this is the dbcp configuration
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="1" />
<property name="idleMaxAgeInMinutes" value="1" />
<property name="maxConnectionsPerPartition" value="3" />
<property name="minConnectionsPerPartition" value="1" />
<property name="partitionCount" value="1" />
<property name="acquireIncrement" value="5" />
<property name="acquireRetryAttempts" value="50" />
<property name="acquireRetryDelayInMs" value="1000" />
<property name="queryExecuteTimeLimitInMs" value="5000" />
<property name="connectionTestStatement" value="SELECT count(*) FROM dbo.sysobjects" />
<property name="closeConnectionWatch" value="true" />
<property name="lazyInit" value="false" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>

Related

Connection pooling in spring jdbc

I have implemented spring jdbc in my project. I am just curious to know how connection pooling in handled in spring jdbc? If spring is taking care of connections, then where can I specify the max number of connections allowed for my application?
Another question is how is connection pooling handled in simple jdbc.
Please clarify.
You can use own custom datasource, like this:
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:SPRING_TEST" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
//Dao class configuration in spring
<bean id="EmployeeDatabaseBean" class="com.test.EmployeeDAOImpl">
<property name="dataSource" ref="springDataSource"/>
</bean>
Official documentation
Here or here described pretty simple.

Converted JNDI name [java:comp/env/***] not found before reading the properties file

Good Mornig.
I have an application in tomcat 8, java 1.7, Spring 3.2.2 and primefaces 5.0. When the application starts, it shows the next debug message in console:
Looking up JNDI object with name [java:comp/env/etl.csv.token]
localhost-startStop-1: org.springframework.jndi.JndiLocatorDelegate
Converted JNDI name [java:comp/env/etl.csv.token] not found
But the key isn't in the JNDI context. It is in a properties file. The application works fine and takes the property from the properties file, but i can't understand why spring is trying to find the property in the JNDI context
The properties file is like this:
database.driverClassName=org.postgresql.Driver
database.initialSize=3
database.maxActive=15
database.maxIdle=10
database.minIdle=3
database.maxWait=10000
etl.csv.token=,
And its properties are used like this:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="initialSize" value="${database.initialSize}" />
<property name="maxActive" value="${database.maxActive}" />
<property name="maxIdle" value="${database.maxIdle}" />
<property name="maxWait" value="${database.maxWait}" />
<property name="minIdle" value="${database.minIdle}" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT CURRENT_TIMESTAMP" />
<property name="removeAbandoned" value="true" />
</bean>
For all properties, the behaviour is the same. I would like that message does not appear
Sorry for the english, is not my native language
Any help will be apreciated.

ERROR: Communications link failure in MYSQL using JPA and Hibernate

I am developing an application where persistence is done via JPA and Hibernate 4.2.3. everything is working normally the first moment.
However when you spend a certain time (in my case one day) application aprensenta the following error.
ERROR: Communications link failure
The last packet successfully received from the server was 259.217.434 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
Abaixo deixo meu persistence.xml...
<persistence-unit name="amh_sys" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/amh_sys" />
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.jdbc.wrap_result_sets" value="false" />
<property name="hibernate.hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.c3p0.validate" value="true" />
</properties>
</persistence-unit>
I tried to configure using C3P0 connection pool, however had the same error ... Below configuration of C3P0.
<!-- Important -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
I can force the error by changing the date on your computer, because MySQL gets the same date as a reference. I can not solve this problem, I hope someone here had the same problem trying to share your solution applied.
Felipe.
The MySQL server will timeout the connect if it has been idle for too long. http://dev.mysql.com/doc/refman/5.0/en/gone-away.html You can do use a scheduled query within the timeout to keep the connect active or re-establish the connection by closing and re-creating the EntityManager

DBCL message on console

I am creating a database connection pool with following properties.
<bean id="complianceCCRDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="false" >
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="username" value="${deshaw.compliance.ccr.db.username}" />
<property name="password" value="${deshaw.compliance.ccr.db.password}" />
<property name="url" value="${deshaw.compliance.ccr.db.url}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxWait" value="60000"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery">
<value>use ${deshaw.compliance.ccr.db.name} SELECT 1
</value>
</property>
<property name="maxIdle" value="10" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<property name="logAbandoned" value="false" />
</bean>
When I try to instantiate the above bean, I am getting following messages on STDOUT. Is there any way I can suppress these ?
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#2d82ef6b)
LogAbandoned: false
RemoveAbandoned: true
RemoveAbandonedTimeout: 600
You should use the setLogWriter() (see javadoc here) method as described in this JIRA.

Configure spring datasource for hibernate and #Transactional

At this moment I'm using DriverManagerDataSource with #Transactional annotation to manage transactions. But all transactions are very very slow, probably because data source open and close connection to db each time.
What data source should I use to speed up transaction?
I am using in my application combination of two approaches. the first one is c3p0 connection pooling, its almost the same solution as chkal sugested. The second approach is to use Spring lazyConnectionDataSourceProxy, which creates lazy loading proxy that loads connection only if you hit the database. This is very useful, when you have second level cache and you are only reading cached data and queries - database wont be hit, and you don't need to acquire connection (which is pretty expensive).
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Pool properties -->
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="120" />
<property name="maxIdleTime" value="1200" />
</bean>
<bean name="lazyConnectionDataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="dataSource" />
</bean>
DriverManagerDataSource isn't actually a connection pool and should only be used for testing. You should try BasicDataSource from Apache Commons DBCP. Something like:
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

Categories