Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Workshop, JPA, and DataSources

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

James Bayer's Blog | January 25, 2008  11:35 PM | Comments (2)


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

Comments are listed in date ascending order (oldest first) | Post Comment

  • karthik, It looks like you're trying to using TopLink. Are you sure that the classes for oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider are on your path. By default, only OpenJPA / Kodo libraries will be there in Workshop or WLS. Thanks, James

    Posted by: jbayer on February 24, 2008 at 8:53 PM

  • hieee.. I am having a problem in JPA where in i connect to a MySQL DB and my WebServer is WebLogic 9.x I get a persistence Provider Not found exception Here is my persistence.xml oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider com.merchandise.usermanagement.enterprisebeans.UserBean Is there something wrong here?? Any help wud be great.. Thanks Karthik

    Posted by: karthik_saravanan on February 21, 2008 at 3:54 AM



Only logged in users may post comments. Login Here.

Powered by
Movable Type 3.31