I used this article for an application.
Developing and running data access applications for the Liberty profile using WebSphere Application Server Developer Tools for Eclipse
I found I had to make some adjustments for my version.
First I got a a DB2 connection working and then MySQL
The key files:
server.xml
<dataSource id="db2" jndiName="jdbc/MyDataSource" type="javax.sql.DataSource">
<jdbcDriver libraryRef="DB2JCC4Lib"/>
<properties.db2.jcc databaseName="SAMPLE" user="user" password="{xor}MC9aaa="></properties.db2.jcc>
</dataSource>
<library id="DB2JCC4Lib">
<fileset dir="c:/PROGRA~1/IBM/SQLLIB/java" includes="db2jcc4.jar,db2jcc_license_cu.jar"/>
</library>
This establishes the database credentials, the required jars and an JNDI name. Server.xml is straight forward.
Persistance.xml
<persistence-unit name="CustomerQuery"><jta-data-source>java:comp/env/jdbc/SAMPLE</jta-data-source>
<class>com.ibm.devworks.CustomerAccount</class>
<!-- exclude-unlisted-classes>true</exclude-unlisted-classes -->
<properties>
<property name="openjpa.LockTimeout" value="30000" />
<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" />
<property name="openjpa.Log" value="none" />
<property name="openjpa.jdbc.UpdateManager" value="operation-order" />
<property name="openjpa.jdbc.Schema" value="myschema"/>
<property name="openjpa.ConnectionDriverName" value="com.ibm.db2.jcc.DB2DataSource"/>
</properties>
</persistence-unit>
There are a few things to note in this file:
-OpenJPA required the openjpa.ConnectionDriverName property and I was required to place the db2jcc4.jar within the Web Application lib directory in order for the class to be found.
- The jta-data-source includes a java:comp/env prefix. This is a resource reference defined in web.xml of the Web Application and not the JNDI name found in server.xml
web.xml
<resource-ref>
<description>connection to SAMPLE</description>
<res-ref-name>jdbc/SAMPLE</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<mapped-name>jdbc/MyDataSource</mapped-name>
<lookup-name>jdbc/MyDataSource</lookup-name>
</resource-ref>
The things to note in this file:
The res-ref-name is set to jdbc/SAMPLE. This is the name that is listed in persistance.xml as
<jta-data-source>java:comp/env/jdbc/SAMPLE.
The lookup-name points to the JDNI name of the datasource listed in the server.xml.
Code
Now you should be able to get a EntityManagerFactory in your code.
@PersistenceUnit(unitName = "CustomerQuery")
EntityManagerFactory emf;
MySQL Setup
To use MySQL, I created a new persistence-unit as shown below:Persistance.xml
<persistence-unit name="CustomerQueryMySQL"><jta-data-source>java:comp/env/jdbc/SAMPLE</jta-data-source>
<class>com.ibm.devworks.CustomerAccount</class>
<!-- exclude-unlisted-classes>true</exclude-unlisted-classes -->
<properties>
<property name="openjpa.LockTimeout" value="30000" />
<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" />
<property name="openjpa.Log" value="none" />
<property name="openjpa.jdbc.UpdateManager" value="operation-order" />
</properties>
</persistence-unit>
In this case, I did not have to provide a driver property.
web.xml
<resource-ref>
<description>
Auto Generated - SDO Datasource connection to SAMPLE</description>
<res-ref-name>jdbc/SAMPLE</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<lookup-name>jdbc/MySQLDataSource</lookup-name>
</resource-ref>
The JNDI name in the lookup-name property has changed to point to the MySQL database.
server.xml
<dataSource jndiName="jdbc/MySQLDataSource" type="javax.sql.DataSource">
<jdbcDriver>
<library>
<fileset dir="C:/software/mysql/lib" includes="mysql-connector-java-5.1.26-bin.jar"/>
</library>
</jdbcDriver>
<properties databaseName="test" password="pass" portNumber="3306" serverName="localhost" user="nemo"/>
</dataSource>