Suggested Pages

Thursday, June 14, 2012

MySQL Datasource for JBoss 6.x

To configure a datasource for MySQL on JBOSS 6.x, you only need only three steps:

Step-1: Copy MySQL driver to server lib

Copy MySQL driver ( for example mysql-connector-java-5.1.19-bin.jar) into <JBoss-Home>/server/<jboss-server-configuration>/lib/

Step-2: Create mysql-ds.xml

Create a file mysql-ds.xml and put it into the directory <JBoss-Home>/server/<jboss-server-configuration>/deploy/.
mysql-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
   
    <jndi-name>myconnection</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/myschema</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>simone</user-name>
    <password>simone</password>

    <min-pool-size>2</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <idle-timeout-minutes>5</idle-timeout-minutes>

    <exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
    <valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>

  </local-tx-datasource>
</datasources>



Step-3:Use JNDI name in persistence.xml


Now you can use the JNDI name (java:/myconnection) configured in the previous step inside you persistence.xml.
persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
 version="2.0">
 <persistence-unit name="persistenceUnitTest">
  <jta-data-source>java:/myconnection</jta-data-source>
  <properties>
   <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
   <property name="hibernate.hbm2ddl.auto" value="create-drop" />
   <property name="hibernate.show_sql" value="true" />
   <property name="hibernate.format_sql" value="true" />
  </properties>
 </persistence-unit>
</persistence>

No comments :

Post a Comment

Suggested Pages