Configuring DataSourcesPage edited by Jean-Louis MONTEIRODocumentation of the ciphering password featureConfiguring DataSources in openejb.xmlThe <Resource> 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 using <Resource> to configure a DataSource. The <Resource> element is designed after @Resource annotation and has similar attributes. For example, this annotation in your bean:
@Resource(name = "myDerbyDatasource", type = javax.sql.DataSource.class)
Command line toolApache OpenEJB also provides a command line tool allowing password cipher algorithm. Actually, it's useful to get the ciphered value of a plain text value using a given algorithm. NAMEopenejb cipher - OpenEJB Cypher Tool SYNOPSISopenejb cipher options <value> DESCRIPTIONThe OpenEJB Cipher tool is an OPTIONAL tool that allows you to use PasswordCipher algorithm to encode/decode values. It can be used to deploy into an offline server, however in this scenario it simply copies the archive into the openejb.base/apps directory which is something that can be done manually with a simple copy command or drag and drop. The OpenEJB Cipher tool can be executed from any directory as long as <OPENEJB_HOME>/bin is in the system PATH. Before running this tool you need to set the environment variable OPENEJB_HOME to the path of the directory where you unpacked the OpenEJB installation. For for the remainder of this document we will assume you unpacked OpenEJB into the directory C:\openejb-3.1.2. In Windows, the cipher tool can be executed as follows: C:\openejb-3.1.2> bin\openejb cipher --help In UNIX, Linux, or Mac OS X, the cipher tool can be executed as follows: [u...@host openejb-3.1.2]# bin/openejb cipher --help Depending on your OpenEJB version, you may need to change execution bits to make the scripts executable. You can do this with the following command. [u...@host openejb-3.1.2]# chmod 755 bin/openejb From here on out, it will be assumed that you know how to execute the right openejb script for your operating system and commands will appear in shorthand as show below. openejb cipher --help OPTIONS
EXAMPLESEncrypt a plain password using the default algorithm. Output xMH5uM1V9vQzVUv5LG7YLA== Configurations for some commonly used databases:HSQLDBThe drivers are included with OpenEJB 3.0 and HSQLDB is the default database. <Resource id="HSQLDB Database" type="DataSource"> JdbcDriver org.hsqldb.jdbcDriver JdbcUrl jdbc:hsqldb:file:hsqldb UserName sa Password </Resource> Derby (Embedded)<Resource id="Derby Database" type="DataSource"> #Embedded Derby example JdbcDriver org.apache.derby.jdbc.EmbeddedDriver JdbcUrl jdbc:derby:derbyDB;create=true UserName admin Password pass </Resource> MySQL<Resource id="MySQL Database" type="DataSource"> # 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 </Resource> Oracle<Resource id="Oracle Database" type="DataSource"> # 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 oracle.jdbc.OracleDriver JdbcUrl jdbc:oracle:thin:@localhost:1521:orcl UserName scott Password tiger </Resource> PosgreSQL<Resource id="PostgreSQL Database" type="DataSource"> # 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 </Resource> InstantDB<Resource id="InstantDB Database" type="DataSource"> # InstantDB example # JdbcDriver org.enhydra.instantdb.jdbc.idbDriver JdbcUrl jdbc:idb:conf/instantdb.properties UserName Admin Password pass </Resource>
JNDI names for configured DataSourcesExample 1<Resource id="Default JDBC Database" type="DataSource"> . . . . . </Resource> The global jndi name would be java:openejb/Resource/Default JDBC Database Example 2<Resource id="Derby Database" type="DataSource"> . . . . . </Resource> The global jndi name would be java:openejb/Resource/Derby Database Obtaining a DataSourceDataSource references in your ejb should get automatically mapped to the Resource you declare. The shortest and easiest rule is that if your reference name matches a Resource in your openejb.xml, that's the one you get. Alternatively, you can explicitly set them via an openejb-jar.xml. There are various ways one could obtain a DataSource now. Lets take an example of Derby. With a Resource declaration in your openejb.xml like this: <Resource id="myDerbyDatabase" type="DataSource"> . . . . . </Resource> There are several possible ways to refer to it in your bean code or ejb-jar.xml
@Stateless
public class FooBean {
@Resource DataSource myDerbyDatabase;
}
OR @Stateless public class FooBean { @Resource(name="myDerbyDatabase") DataSource dataSource; } OR @Resource(name="myDerbyDatabase", type="javax.sql.DataSource") @Stateless public class FooBean { public void setSessionContext(SessionContext sessionContext) { DataSource dataSource = (DataSource) sessionContext.lookup("myDerbyDatabase"); } public void someOtherMethod() throws Exception { InitialContext initialContext = new InitialContext(); DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/myDerbyDatabase"); } } 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>
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSources confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
- [CONF] OpenEJB 3.0.x documentation > Configuring DataSo... confluence
