Our application always demands the datasource setup in the server side to manager all our database connections.
We need to understand the modules available in JBoss before even we are starting the setup. Modules folder is located under \JBOSS-HOME\modules under which you can see lot of packaged structure which starts like com, org, etc.. There we need to copy our desired jar files by creating or copying it under right hierarchy.
In my instance,
1. Deploy Driver / Module of DB
I copied my postgresql-8.4-701.jdbc3.jar file to c:\jboss\modules\org\postgresql\main and created module.xml with following content
<jboss:module:1.0″ name=”org.postgresql”>
<resources>
<resource-root path=”postgresql-8.4-701.jdbc3.jar”/>
</resources>
<dependencies>
<module name=”javax.api”/>
<module name=”javax.transaction.api”/>
</dependencies>
</module>
Then, try restarting the JBoss, you should be able to see the postgresql driver deployed and your postgresql-8.4-701.jdbc3.jar.index created in the same directory where you copied your jar file.
2. Configure DB Driver
Then after successful deployment of you driver module, its time to edit your standalone.xml or domain.xml by appending following piece of xml code to it,
<driver name=”org.postgresql” module=”org.postgresql”>
<xa-datasource-class>
org.postgresql.xa.PGXADataSource
</xa-datasource-class>
</driver>
Copy this information under subsystem>datasources element. Now, it means that you have included the same driver for the standalone startup. This enables the user to add the datasource to his server. This can be achieved in may ways, thought admin console in web, or programatically by editing standalone.xml / domain.xml or by CLI.
I will explain how to add datasource programatically and test it in CLI and sample java code.(contact me if you need steps to deploy by other means).
3. Configure Datasource in standalone.xml / domain.xml
Edit your standalone.xml present in c:\jboss\standalone\configuration folder. There add following lines of code under subsystem>datasources element
<datasource jndi-name=”java:jboss/datasources/Test” pool-name=”java:jboss/datasources/Test_Pool” enabled=”true” jta=”true” use-java-context=”true” use-ccm=”true”>
jdbc:postgresql://192.168.1.192:5444/testdb
<driver>org.postgresql</driver>
<security>
<user-name>testuser</user-name>
<password>testpassword</password>
</security>
</datasource>
Note: Make sure the datasource name has the prefix of java:\ or java:jboss\ or else, your datasource cannot be referenced.
Then with that, you are done on the configuration side. Start/restart your server where you can notice in console that the datasource is started.
4. Testing Datasource
4.1 Testing the datsource in CLI
Connect your CLI to your local server by the following code
C:\jboss\bin>jboss-admin.bat
You are disconnected at the moment. Type ‘connect’ to connect to the server or ‘help’ for the list of supported commands.
[disconnected /] connect
Connected to standalone controller at localhost:9999
[standalone@localhost:9999 /]Then execute the following code,
[standalone@localhost:9999 /] /subsystem=datasources/data-source=java\:jboss\/datasources\/Test:test-connection-in-pool
{
“outcome” => “success”,
“result” => [true]
}
4.2 Testing datasource in JAVA Code
Note: You cannot test datasource from a standalone java api. You should put that code / java api in side a war / web application and deploy in to the JBoss where you have deployed the data source and try it.
In my case, I have created a servlet called TestServlet and in GET method I have added the following lines of code
DataSource ds = null;
Context ctx = null;
try {
String strDSName = “java:jboss/datasources/Test”;
ctx = new InitialContext();
ds = (javax.sql.DataSource) ctx.lookup(strDSName);
resp.getWriter().print(“Success getting DS : ” + ds.getClass());
} catch (Exception e) {
resp.getWriter().print(“Error getting DS : ” + e);
}
Try calling the servlet by giving its context/servlet path which should print the success message.
Thanks.
Kousik Rajendran.
I tried to set up a mysql datasource on a cluster using domain.xml, and the domain master starts properly, but when I start the slave, it dies with a Too many connections error. This is with no WAR file deployed, the slave makes an apparently infinite number of connections to the database server on startup. Any ideas on what I am doing wrong?
Pingback: Mistakes in previous article about installing PostgreSQL JDBC-driver in jBoss | Grey Wraith