3

I'm using Spring Hibernate template apache tomcat and MySQL. Everything works great in development, but when I deploy the app to a test server and let it run (largely idle) overnight, I'm usually greeted with this in the morning:

Following is my spring servlet.xml:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
      <property name="maxUploadSize" value="1000000"/>
</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

How can i configure the spring servlet.xml to avoid this problem ? Thanks.

jan's
  • 31
  • 1
  • 1
  • 4

3 Answers3

1

you can use hibernate properties to set pool of sessions and can also configure timeout.

<property name="hibernate.c3p0.timeout">300</property>.

hope this will help you.

  • for more details see this : https://forum.hibernate.org/viewtopic.php?t=279&start=0&postdays=0&postorder=asc&highlight=&sid=7985f21867f3065736f9ee239f84c6c6 – Manjeet Rulhania Jul 04 '14 at 08:49
  • ok thank you...but where i can include this code inside my spring-servlet??. iam using hibernate template of spring frame-work,... – jan's Jul 04 '14 at 09:30
1

Hi @jans it is said we should not use hibernate built-in connection poolin production. You can use C3p0 for connection pooling to avoid the error. Also from something I learned just make sure you are closing all the sessions you used. You can add below code in your "hibernateProperties" property.

<prop key= "hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>      
<prop key="hibernate.c3p0.acquire_increment">5</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
0

You need to check the SQL logs. The session has timed-out because some queries are taking too long than the default Transaction timeout.

Giving it a larger value won't resolve your problem. So, you need to configure a tool like jdbcdslog to intercept all your queries and log the slow ones.

This way you can know for sure what's taking so long. It can be one inefficient query or a lot of small ones (N + 1 select) which would be better optimized with a join instead.

This way you can also figure out which queries need more indexes.

Alternatively, you can plan on setting up a second level query cache to speed-up some sloq queries which seldom change.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911