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.
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

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ -->
<!--  Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
  <local-tx-datasource>
    <jndi-name>myDataSource</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>123</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
    -->
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>
hibernate.cfg.xml
 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.datasource">java:myDataSource</property>
        <!--<mapping class="com.sameera.domain.User" />-->
      
    </session-factory>

</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