Configuring JBoss Datasource for Firebird DB

Accessing a relational database system from Java is a basic step required for many applications. The JEE architecture defines a standard for gaining this access, calls Java Connector Architecture (JCA). This article is a short HOWTO of configuring JCA-compliant datasource to a Firebird 2.x RDBMs using JBoss AS 5.1.0 as example. This tutorial is based on a Windows installation, but can be easily ported to Linux, or other OS.
For the configuration of the datasource two steps are required:
- Deployment of the Firebird RAR resource adapter (
jaybird-*.rar) - Creation of the
firebird-ds.xmlconfiguration
RAR Resource Adapter
The RAR Resource Adapter is a version of RDBMs drivers packaged in a special way, defined by the JCA specifcation. The
latest versions of Type 4 JCA-JDBC Firebird Driver (JayBird) can be downloaded from the IBPhoenix site. Inside of the downloaded archive you will find jaybird-*.rar (The version used during the creation of this tutorial was jaybird-2.1.6.rar). Since deployment in JBoss AS is performed by simple file copy, make sure to put the RAR-adapter-file into the deployment location of the application server. (e.G. ${JBOSS_ROOT}\server\default\deploy\).
After deployment, you can check the status of the resource adapter, by looking on the Administration Console of the JBoss (by default accessing the server URL http://localhost:8080/admin-console/ if run locally).
Datasource Description
After a successful deployment of the Resource RAR, the configuration of the datasource has to be created. The Firebird datasource configuration is supplied in a file, located in the default deployment location. JBoss AS auto-deplyoer will automatically look for the *-ds.xml files, so we name it e.G. firebird-ds.xml.
The following datasource configuration defines a local transactional datasource. You just need to replace placeholders USERNAME, PASSWORD and path to your database-file (In the used installation the file was located in: c:\databses\tesdb.fdb)
<?xml version="1.0" encoding="UTF-8"?>
<!-- ==================================================================== -->
<!-- New ConnectionManager setup for firebird dbs using jca-jdbc xa driver-->
<!-- Build jmx-api (build/build.sh all) and view for config documentation -->
<!-- ==================================================================== -->
<connection-factories>
<local-tx-datasource>
<jndi-name>DataSourceFirebirdDS</jndi-name>
<connection-url>jdbc:firebirdsql:localhost/3050:c:${/}databses${/}tesdb.fdb</connection-url>
<driver-class>org.firebirdsql.jdbc.FBDriver</driver-class>
<transaction-isolation>TRANSACTION_REPEATABLE_READ</transaction-isolation>
<connection-property name="lc_ctype" type="java.lang.String">ISO8859_1</connection-property>
<connection-property name="maxStatements">10</connection-property>
<user-name>USERNAME</user-name>
<password>PASSWORD</password>
<min-pool-size>0</min-pool-size>
<max-pool-size>10</max-pool-size>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>15</idle-timeout-minutes>
<check-valid-connection-sql>SELECT CAST(1 as INTEGER) FROM rdb$database</check-valid-connection-sql>
<track-statements>false</track-statements>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
<metadata>
<type-mapping>Firebird</type-mapping>
</metadata>
</local-tx-datasource>
</connection-factories>
Usage
The datasource in the example above is registered in the java JNDI namespace and is called DataSourceFirebirdDS. In the source code, this resource should be accessible via java:/DataSourceFirebirdDS. Here is the example using Dependency Injection (DI):
@Ressource(mappedName = "java:/DataSourceFirebirdDS") private DataSource firebirdDataSource;
In order to use the datasource in a JPA, the persistence unit has to be configured. For example, for using Hibernate, the following persistence unit can be configured.
... <persistence-unit name="testunit"> <jta-data-source>java:/DataSourceFirebirdDS</jta-data-source> <provider>org.hibernate.ejb.HibernatePersistence</provider> ... <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.FirebirdDialect"/> <property name="hibernate.show_sql" value="true" /> ... </properties> </persistence-unit> ...
Then in the DAO for manipulation of Persistent Entities, the Persistence Context can be injected by:
@Stateless
public class TestDAOBean implements TestDAO {
@PersistenceContext(unitName = "testunit")
private EntityManager manager;
...
}
Have fun and please provide some feedback.
7 Responses to “Configuring JBoss Datasource for Firebird DB”
By Heiko Tappe on Jan 4, 2010 | Reply
I really loved to see/read something about Firebird and JEE. Give us more!
BTW, why don’t you use “FirebirdDialect” in your persistence unit?
Another example with an xa-datasource would be nice. At some point I used the following and it worked for me. But it might need some tweaking to work correctly. And I remember that I had to put hands on the Jaybird code to get it working. But can’t remember what it was. I talked to Roman Rokytskyy about it that time.
<xa-datasource> <jndi-name>FirebirdXADS</jndi-name> <xa-datasource-property name=”Database”>localhost:c:\database\firebird.fdb</xa-datasource-property> <xa-datasource-property name=”Encoding”>UTF8</xa-datasource-property> <xa-datasource-class>org.firebirdsql.pool.FBConnectionPoolDataSource</xa-datasource-class> <user-name>itsme</user-name> <password>mypassword</password> <min-pool-size>5</min-pool-size> <max-pool-size>20</max-pool-size> <track-statements>true</track-statements> <metadata> <type-mapping>Firebird</type-mapping> </metadata> </xa-datasource>Cheers,
Heiko
By Simon Zambrovski on Jan 4, 2010 | Reply
Thanks for the hint. Of course the Firebird dialect should be used. I fixed it in the article.
By shuron on Jan 4, 2010 | Reply
Thanks for dialect hint.
Thank you anyway!
XA is also an interesting point for the future, because I work only with one DB at the moment.
Maybe you’ll remember what has to be changed in Jaybird code to support XA.
Give us a hint if you did
By Heiko Tappe on Jan 5, 2010 | Reply
I found it
You can read about the Jaybird/JBoss problem in the firebird java newsgroup (thread “IllegalStateException (JBoss, Timeout)”).
<snip>
But I found out that commenting line #285 in AbstractPingablePooledConnection.java seems to “solve” the problem.
</snip>
By shuron on Jan 6, 2010 | Reply
Ok
A link would be easier 
maybe i’ll tiry it out.
Thank you anyway
But first i will test existing configuraton on Debian Linux.
By shuron on Jan 27, 2010 | Reply
I could successfully test it also on debian linux.
By the way, here ist short tutorial: how to install Jboss on debian linux
By shuron on Feb 11, 2010 | Reply
Jboss community has also very good site on DataSource configuration for different Databases,
So check it out:
http://community.jboss.org/wiki/configdatasources