This post will describe the steps necessary to configure a JDBC DataSource in JBoss 7. MySQL database and JBoss 7.1.0-Final will be used as example.
Configuring MySQL JDBC driver module
The first big change on JBoss 7 compared to the previous versions is that the libraries to be deployed are no longer considered “libs”, in the sense that all the required work was to copy the .jar files to a specific folder and start using the services provided by that .jar. Any lib is now considered a JBoss module, like the server’s internal services, such as messaging. The following steps are required to configure the MySQL JDBC driver:
- Download the MySQL JDBC driver from http://dev.mysql.com/downloads/connector/j/)
- Create the folder com/mysql/main inside JBOSS_HOME/modules
- Copy the JDBC driver to the folder created above
- Create a file named module.xml inside JBOSS_HOME/modules/com/mysql/main/, with the following content (considering that mysql-connector-java-5.1.17-bin.jar is the driver used):
<?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.0" name="com.mysql"> <resources> <resource-root path="mysql-connector-java-5.1.17-bin.jar"/> </resources> <dependencies> <module name="javax.api"/> </dependencies> </module>
This file is responsible for registering the module within JBoss, describing the module location (com.mysql), the module file name and its dependencies.
There is still other ways to configure the driver, but this post will only discuss this option.
Having done that, the module is already available and the DataSource can be configured.
DataSource configuration
This JBoss version no longer has the old structure with configuration files widespread across the directories. Instead, there is only one file with the required configurations:
JBOSS_HOME/standalone/configuration/standalone.xml (by default). All the services, such as JMS, Logging, DataSources, and so on, are configured within this file.
In order to configure the DataSource, open the standalone.xml file, scroll down until the datasource subsystem section (<subsystem xmlns=”urn:jboss:domain:datasources:1.0″>) and add a new <datasource> inside <datasources>:
<datasource jta="true" jndi-name="java:/test-ds" pool-name="test" enabled="true" use-java-context="true" use-ccm="true"> <connection-url>jdbc:mysql://localhost:3306/<databasename></connection-url> <driver>com.mysql</driver> <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation> <pool> <min-pool-size>5</min-pool-size> <max-pool-size>30</max-pool-size> <prefill>true</prefill> <use-strict-min>false</use-strict-min> <flush-strategy>FailingConnectionOnly</flush-strategy> </pool> <security> <user-name><user></user-name> <password><password></password> </security> <statement> <prepared-statement-cache-size>32</prepared-statement-cache-size> </statement> </datasource>
The <datasource>’s configuration attributes are pretty much the same of the previous versions, and more information regarding that can be found here.
Right below that, in the <drivers> section, it is necessary to add the MySQL driver (previously configured);
<driver name="com.mysql" module="com.mysql"> <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> </driver>
At this point, the DataSource is ready to be utilized by other services and applications.
Just out-of curiosity, it is also possible to configure a DataSource using the old approach (creating a file with suffix *-ds.xml inside deployments directory) or programmatically, through the @DataSourceDefinition annotation.
That’s it, now the DataSource can be normally used by your applications.
Parabéns cara, ficou bem melhor que a maiorias dos post que eu vi por ai sobre DS no JBoss AS 7, inclusive o que eu tinha feito rsrs
Abraço.
LikeLike
Obrigado Maurício, que bom que gostou!
Já tinha me “deparado” com seu blog algumas vezes, tem bastante informação lá, show de bola!
Abraços
LikeLike
Luciano ,
O post ficou excelente totalmente objetivo e bem escrito. Parabéns
Abraço.
LikeLike
Oi Ricardo,
Que bom que gostou, obrigado
LikeLike
Boa. Esclareceu bastante!
LikeLike
Cara muito obrigado pelo tutorial, quebrou um galhão, muito bom mesmo.
LikeLike
Opa Igor, que bom que o tutorial foi útil pra você.
LikeLike
Muito bom o post tive uma boa ideia de como fazer, mas estou quebrando a cabeça para criar o datasource para firebird… se poderem ajudar agradeço..
LikeLike
Oi André..infelizmente nunca fiz para o firebird, mas acredito que a lógica é a mesma.
LikeLike
Bah, Perfeito este post, amigo!
Ficou realmente muito claro e fácil de configurar.
Consegui fazer aqui em precisar fazer nenhum outro tipo de ajuste fora do que foi mencionado.
Parabéns!!
LikeLike
Marcelo,
Obrigado pelo comentário. Fico feliz que tenha sido útil.
Abraço.
LikeLike
Muito obrigado, brou! Quebrou um galhão também!!!
Hallowed be thy name!!!! =)
LikeLike
Fala Danielson,
Que bom que foi útil!
Abraço
LikeLike
PESSOAL
Os conectores versão 5.1.30+ precisam declarar no standalone.xml
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
com.mysql.jdbc.Driver
LikeLike
No WildFly 8.1 segue a mesma configuração?
LikeLike
Olá Emerson, é pra funcionar sim.
LikeLike
Reblogged this on carledwin & Java and commented:
Olá, este foi um dos posts que me auxiliou em minha publicação. Segue o link.
LikeLike
Obrigado pela referência!
LikeLiked by 1 person
Oi cara, bom dia.
No arquivo *-ds.xml o que deve estar nos campos “jndi-name” e “pool-name”? Estou usando um arquivo gerado pelo seam-gen e tendo erros de missing dependencies por causa do datasource.
LikeLike
O valor que informar no jndi-name é o que vai user no seu projeto para fazer o link..o valor do pool-name é irrelevante.
LikeLike