Search code examples
tomcatjdbcjbossjndi

Using Tomcat connection pool in JBoss AS 5.x


How can I create ConnectionPool/JDBC in JBoss without change any code in my application? I already have the deploy file (.WAR).

For example, in tomcat I just needed to add a new resource to the server.xml file. Something like that:

...
<GlobalNamingResources>
    ...
    <Resource driverClassName="com.mysql.jdbc.Driver" name="testDBConnectionPool" password="root" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/testDB" username="root"/>
    ...
</GlobalNamingResources>
...

How to do it in JBoss? As I can understand, JBoss uses mysql-ds.xml file for that but we need to call it in the code. Am I right?

Is there any similar approach like Tomcat in JBoss?


Solution

  • add required info in mysql-ds.xml and put it in $JBOSS_HOME/dirserver/default/deploy .

    If your datasource name is jdbc/MySQLDS , then you can call it like

    javax.naming.Context ic = new javax.naming.InitialContext();
    javax.naming.Context ctx = (javax.naming.Context) ic.lookup("java:");
    javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/MySQLDS");
    java.sql.Connection con = ds.getConnection();
    

    update:

    you need to make sure mysql-ds.xml has the exact values which are there in server.xml of tomcat.