« Hermes JMS - Open Source JMS Console | Main | Paparazzi for Programming Languages »

Workshop, JPA, and DataSources

This was originally posted on my dev2dev blog January 25th, 2008.  BEA Workshop components are soon to be rebranded as the Oracle Eclipse Pack.

One of my customers is evaluating JPA as a persistence framework for a new project and using Workshop 10.1 for development and WebLogic Server 10.0 MP1 for a target runtime.  They had some trouble figuring out how to configure JPA to use WebLogic managed DataSources instead of using a direct OpenJPA managed connection.  In this entry, I'll illustrate how to specify JNDI DataSources in persistence.xml.

Default behavior is direct connection

In Workshop 10.1, when you right click on a table in DbXplorer and select "Generate JPA Mapping...", and target a Web project with JPA facets, Workshop will add the connection information from DbXplorer to the persistence.xml file that uses the direction connection method.  Here's an example of the output for one class called Supplier.  Notice that the direct connection is used and the password is embedded in the file.  This might be ok for development, but this might raise some eyebrows if left unchecked for production.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="pu">
        <class>com.test.supplier.jpa.Supplier</class>
        <properties>
            <property name="openjpa.TransactionMode" value="local"/>
            <property name="openjpa.ConnectionDriverName" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:XE"/>
            <property name="openjpa.ConnectionUserName" value="weblogic"/>
            <property name="openjpa.ConnectionPassword" value="weblogic"/>
            <property name="openjpa.jdbc.Schema" value="WEBLOGIC"/>
        </properties>
    </persistence-unit>
</persistence>

Best practice - use container managed DataSources

Instead of managing all of database connections separately in each application, it is a best practice to use container managed DataSources that are shared among applications.  Here are two data-sources I have configured in the WLS console.

datasources

Using DataSources also has the added benefit of not requiring developers to have access to the database attributes, specifically the password.  It turns out that the Workshop 10.1 JPA plug-in does not directly support specifying the JNDI name of the WebLogic DataSource in the GUI.  Looking at the persistence XSD file or trying a code-completion in the persistence.xml file will show you that the persistent-unit element has a child element named jta-data-source that can be used for specifying the JNDI name.  In fact, there is a second element as well, non-jta-data-source, which is also required if you want transactions to work properly with XA compliant DataSources.  The OpenJPA documentation user-guide does an excellent job explaining this.

Watch your step

This is where I got tripped up.  Unfortunately, there is a bug in Eclipse WTP which prevents the XML Validator from working properly in Web projects.  Therefore, unless you are careful, you can accidentally place the DataSource elements in the incorrect place.  Code-completion still works in the xml document, but it does not prevent you from making a mistake.  In my case, I put the jta-data-source element after the class element.  This results in an error at runtime such as: persistence.xml [Location: Line: 8, C: 20]: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'jta-data-source'. One of '{"http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":properties}' is expected.

Once I put the data-source elements in the valid location, everything worked great at runtime (thank you Pinaki!).  Here is an example of a persistence.xml file that uses the two DataSources correctly and complies with the XSD.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="pu">
        <jta-data-source>OracleXADataSource</jta-data-source>
        <non-jta-data-source>OracleDataSource</non-jta-data-source>
        <class>com.test.supplier.jpa.Supplier</class>
        <properties>
            <property name="openjpa.jdbc.DBDictionary" value="oracle(DriverVendor=oracle)" />
        </properties>
    </persistence-unit>
</persistence>

Conclusion

Hopefully the JPA tooling in Workshop will incorporate the JNDI data-source connection in a future release and incorporate a fix for validating xml files in Web projects.  For more JPA blog entries, I highly encourage Pinaki's blog.  He's got lots of great JPA stuff.

Comments (2)

Fred:

You have any thoughts on how to use JPA without using a web project? I have a direct rmi based swing client and need to use jpa. Only current way I can see in workshop is to create a 'dynamic web project' and then link it with my ejb project and ear projects.

Fred,

I recommend that you search/post about this in the Workshop forum:
http://forums.oracle.com/forums/forum.jspa?forumID=575
or the Enterprise Pack for Eclipse forum:
http://forums.oracle.com/forums/forum.jspa?forumID=578

I seem to remember that JPA support is only enabled for Workshop Dynamic Web projects. However, you may also be able to try out your use case with JDeveloper or Dali.
http://www.eclipse.org/webtools/dali/main.php

Good luck,

James

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

james_bayer.jpg

I am a Senior Sales Consultant covering enterprise customers in and around the Chicago area focusing on middleware and internet technologies. My career began as a Java consultant and architect where I experienced the technology shift toward SOA. I enjoy helping customers solve business problems by applying Oracle technology solutions.

About This Entry

This page contains a single entry from the blog posted on January 25, 2008 7:06 PM.

The previous post in this blog was Hermes JMS - Open Source JMS Console.

The next post in this blog is Paparazzi for Programming Languages.

Many more can be found on the main index page or by looking through the archives.

Top Tags

Powered by
Movable Type and Oracle