0

I am having an application on a tomcat server which is built on spring framework+Hibernate, but when the application,server are idle for 7days and after that if i try to access the application i am getting below error

org.hibernate.exception.JDBCConnectionException: could not execute query

but the mysql is running on the device , where tomcat server is running i have checked it so i restarted the tomcat server then the application is running fine i am not getting any error

my database.xml file for the application running

<bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="${jdbc.url}"/>
   <property name="username" value="${jdbc.username}"/>
   <property name="password" value="${jdbc.password}"/>
</bean>

</beans>

I cant just restart the server when ever i want to access the application is there any permanent solution to this?

Labeo
  • 5,831
  • 13
  • 47
  • 77
  • Worth a read: http://stackoverflow.com/questions/1683949/connection-timeout-for-drivermanager-getconnection –  Nov 18 '15 at 16:53

1 Answers1

2

You do not actually have a connection pool there(which you need). You are hard coding a single connection. So as soon as it is idle for too long that one connection will be dead.

You need to use a database connection pool. Sine you said you are using Tomcat you might look into using its built in option

https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

<Resource name="jdbc/cciDataSource" 
    auth="Container" 
    type="javax.sql.DataSource"
    username="userAcctName"
    password="somePassword"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://someDatabaseHost/databaseName"
    maxActive="15"
    maxIdle="7" 
    validationQuery="select now()"
    maxWait="1000"
    removeAbandoned="true"
    removeAbandonedTimeout="15"
    testOnBorrow="true"
    logAbandoned="true" />
BrianC
  • 1,793
  • 1
  • 18
  • 26