Connection pool in JBoss 7.1/Wildfly 8.2

When deploying production applications, the best way to handle database connections is through a connection pool. Actually, most of JEE containers can manage connection pools, thus, you only request a connection from your application to the server and server is in charge of manage active connections and serve connections when needed. JBoss lets you configure a datasource and then configure a connection pool. The steps are the next:

  1. Register JDBC library as a module
  2. Create a new database driver based on JDBC module
  3. Create a datasource

1. Register JDBC library as a module

I’m using PostgreSQL 9.3 so i have postgresql-9.3-1102-jdbc41.jar . Navigate to JBoss directory. Inside you will find a folder named modules. Here, you will see various modules already present in JBoss or Widlfly. In Wildfly is a little bit different because inside modules folder, you only will find a folder named system. The idea is to separate system modules from your modules. You will have to create your own folders inside it.

The modules follow a java-package-like structure. So, we can create com folder (if it doesn’t exists) and then create the suffix folder, for example postgresql93, and finally (always) a folder named main. This folder contains the module files.

Inside it you need:

  • The jar file or files
  • The module xml file definition

The xml file has the next structure

Inside resources node you specify all the jars needed for your module. Inside dependencies node you specify dependencies, declared as modules. In the attribute name of module node, you write the name of your package without including main folder.

2. Create a new database driver based on JDBC module

After that, you have to declare the module as a JDBC driver to use it to create datasources inside the server. To do that, navigate to <JBOSS_ROOT>\standalone\configuration and edit standalone.xml file, which is the main configuration file. You will find this node:

Inside it, JBoss stores datasources and drivers configuration. Inside it, you will find drivers node. Then, create drive definition:

After you do that, you can save it and then restart server. If everything was configured properly, somewhere inside the server log you will see something like this:

3. Create a datasource 

Now you can restart application server, and enter to the administration console. I’m using Wildfly 8.2 but you will find JBoss 7.1 administration console very similar. In the Datasources section, we can create one by specifying the name inside Wildfly, the JNDI name.

Wildfly Datasources section

After that, where you specify the driver you will be able to see postgresql93.driver as an option. Finally you specify connections parameters like URL, username and password. You can test the connection to check if everything is ok.

Creating datasource

 

Driver choosing

 

img1

In future JAVA tutorials, we will use this connection pool.

  • Овик Костанян

    You foget say, that before

    org.postgresql.xa.PGXADataSource

    must add postgresql93.driver
    else you catch Error.

  • Овик Костанян

    You forgot to say that you need to add it postgresql93.driver
    Before

    org.postgresql.xa.PGXADataSource