Configuring DataSources in openejb.xml
The <Connector> element is used to configure a javax.sql.DataSource. It is also used to configure other resources like Timers, Topics, Queues. We will see some examples of configuring a DataSource. Each <Connector> element has two attributes:
- id - This will ultimately determine the JNDI name
- provider - The provider
Configurations for some commonly used databases:
HSQLDB
The drivers are included with OpenEJB 3.0 and HSQLDB is the default database.
<Connector id="HSQLDB Database" provider='Default JDBC Database'>
JdbcDriver org.hsqldb.jdbcDriver
JdbcUrl jdbc:hsqldb:file:hsqldb
UserName sa
Password
</Connector>
Derby (Embedded)
<Connector id="Derby Database" provider='Default JDBC Database'>
#Embedded Derby example
JdbcDriver org.apache.derby.jdbc.EmbeddedDriver
JdbcUrl jdbc:derby:derbyDB;create=true
UserName admin
Password pass
</Connector>
MySQL
<Connector id="MySQL Database" provider='Default JDBC Database'>
# MySQL example
#
# This connector will not work until you download the driver at:
# http://www.mysql.com/downloads/api-jdbc-stable.html
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://localhost/test
UserName test
</Connector>
Oracle
<Connector id="Oracle Database" provider='Default JDBC Database'>
# Oracle example
#
# This connector will not work until you download the driver at:
# http://otn.oracle.com/software/tech/java/sqlj_jdbc/content.html
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:oracle:thin:@localhost:1521:orcl
UserName scott
Password tiger
</Connector>
PosgreSQL
<Connector id="PostgreSQL Database" provider='Default JDBC Database'>
# PostgreSQL example
#
# This connector will not work until you download the driver at:
# http://jdbc.postgresql.org/download.html
JdbcDriver org.postgresql.Driver
JdbcUrl jdbc:postgresql://localhost/test
UserName postgres
Password pass
</Connector>
InstantDB
<Connector id="InstantDB Database" provider='Default JDBC Database'>
# InstantDB example
#
JdbcDriver org.enhydra.instantdb.jdbc.idbDriver
JdbcUrl jdbc:idb:conf/instantdb.properties
UserName Admin
Password pass
</Connector>
JNDI names for configured DataSources
Example 1
<Connector id="Default JDBC Database" >
. . . . .
</Connector>
The global jndi name would be java:openejb/Connector/Default JDBC Database
Example 2
<Connector id="Derby Database" provider='Default JDBC Database'>
. . . . .
</Connector>
The global jndi name would be java:openejb/Connector/Derby Database
Obtaining a DataSource
DataSource references in your ejb should get automatically mapped to the Connector you declare. You can explicitly set them via an openejb-jar.xml. The shortest and easiest rule is that if your reference name matches a Connector in your openejb.xml, that's the one you get.
There are various ways one could obtain a DataSource now.
Lets take an example of Derby
<Connector id="myDerbyDatabase" provider='Default JDBC Database'>
. . . . .
</Connector>
@Stateless
public class FooBean {
@Resource DataSource myDerbyDatase;
}
OR
@Stateless
public class FooBean {
@Resource(name="myDerbyDatabase")
DataSource dataSource;
}
OR
<resource-ref>
<res-ref-name>myDerbyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
OR
<resource-ref>
<res-ref-name>jdbc/myDerbyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
OR
<resource-ref>
<res-ref-name>someOtherName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<mapped-name>myDerbyDatabase</mapped-name>
</resource-ref>