Data Source - Jboss - Hibernate

Using datasource to connect database

I hosted  Java web app with MySQL database in Jboss server. When every morning  I see it database connection was lost.Then I ran a query to see timeout variables.
  1. SHOW VARIABLES LIKE '%timeout%'  
wait_timout = 28800

28800 = 8 hours.

If there is no request comes after 8 hours  connection will be lost. I tried several solution for this.After using datasource to connect database that problem was solved.

 mysql-ds.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ -->  
  4. <!--  Datasource config for MySQL using 3.0.9 available from:  
  5. http://www.mysql.com/downloads/api-jdbc-stable.html  
  6. -->  
  7.   
  8. <datasources>  
  9.   <local-tx-datasource>  
  10.     <jndi-name>myDataSource</jndi-name>  
  11.     <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>  
  12.     <driver-class>com.mysql.jdbc.Driver</driver-class>  
  13.     <user-name>root</user-name>  
  14.     <password>123</password>  
  15.     <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>  
  16.     <!-- should only be used on drivers after 3.22.1 with "ping" support  
  17.     <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>  
  18.     -->  
  19.     <!-- sql to call when connection is created  
  20.     <new-connection-sql>some arbitrary sql</new-connection-sql>  
  21.       -->  
  22.     <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers  
  23.     <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>  
  24.       -->  
  25.   
  26.     <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->  
  27.     <metadata>  
  28.        <type-mapping>mySQL</type-mapping>  
  29.     </metadata>  
  30.   </local-tx-datasource>  
  31. </datasources>  
hibernate.cfg.xml
  1.  <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  9.         <property name="connection.datasource">java:myDataSource</property>  
  10.         <!--<mapping class="com.sameera.domain.User" />-->  
  11.         
  12.     </session-factory>  
  13.   
  14. </hibernate-configuration>  
mysql-ds.xml file is taken from jboss server.. jboss-5.1.0.GA\docs\examples\jca
'myDataSource' is the name of datasource

I put mysql-connector-java-5.1.5-bin.jar  to the jboss-5.1.0.GA\server\default\lib

No comments:

Post a Comment