Techie:Techie Main/Java/JBoss/Connection Pool

From FFL Wiki
Jump to navigation Jump to search

Follow all these steps to configure a datasource that is portable across servers

Install MySQL drivers

Copy drivers (e.g. mysql-connector-java-5.0.7-bin.jar) to tomcat's lib directory (e.g. /opt/jboss-5.1.0.GA$ cd common/lib/ for all servers or /opt/jboss-5.1.0.GA/server/default/lib for the default server)

Create datasource

Example MySQL datasource which can be accessed with JNDI at java:FantacyDS . This file should be created in the server's deploy directory and the file name must end '-ds.xml' .


<?xml version="1.0" encoding="UTF-8"?>
<!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->
<!-- $Id: mysql-ds.xml 88948 2009-05-15 14:09:08Z jesper.pedersen $ -->
<!--  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>FantacyDS</jndi-name>
        <connection-url>jdbc:mysql://localhost:3306/fantacy</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <user-name>xxxx</user-name>
        <password>yyyy</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>

Configure a web resource reference

Create a file called jboss-web.xml in the web apps WEB-INF directory with the following content. This will be ignored by other app servers so we can deploy this elsewhere without problems.

<jboss-web>
    <resource-ref>
        <res-ref-name>jdbc/FantacyDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:FantacyDS</jndi-name>
    </resource-ref>
</jboss-web>


Use the resource in the web app

Make use of the resource ref in the web apps deployment descriptor, WEB-INF/web.xml . This is standard Java EE config. This datasource will now be available using JNDI at java:comp/env/jdbc/FantacyDS .

        <!-- DataSource resource -->
        <resource-ref>
                <description>FFL DataSource</description>
                <res-ref-name>jdbc/FantacyDS</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
        </resource-ref>

Note: this is the same web.xml config as we used for tomcat confirming that this is portable across servers