users@glassfish.java.net

Re: JPA portability with JNDI names

From: Dan Allen (mojavelinux) <"Dan>
Date: Tue, 25 Nov 2008 09:37:15 -0800 (PST)

glassfish-2 wrote:
>
> So, the only way to use the same persistence.xml file in two application
> servers is by defining the JDBC connection by vendor specific properties,
> am I right?
>

No. However, I'll admit that this is one of those quirky things that JBoss
AS does to make things easier for developers when in fact it just makes
things more difficult.

By default, JBoss AS binds elements to a localized JNDI context using a
prioprietary java:/ prefix, which differs from the global java:comp/env that
most servers (and even JBoss AS) use. If you configure the -ds.xml file in
the default way, JBoss AS automatically prepends the java:/ prefix.

<local-tx-datasource>
    <jndi-name>vehiclesDS</jndi-name>
    <connection-url>jdbc:mysql://localhost/vehicles</connection-url>
    ...
</local-tx-datasource>

In this case, the JNDI resoruce is java:/vehiclesDS.

What you want to do is disable this proprietary prefix using the
<use-java-context> element in the -ds.xml file:

<local-tx-datasource>
    <jndi-name>jdbc/vehicles</jndi-name>
    <use-java-context>false</use-java-context>
    <connection-url>jdbc:mysql://localhost/vehicles</connection-url>
    ...
</local-tx-datasource>

Now, you can grab the datasource from the global JNDI tree, which is exactly
what happens when you specify the resource in the persistence.xml file (the
java:comp/env is understood):

<persistence-unit name="vehicles" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/vehicles</jta-data-source>
    ...
</persistence-unit>

So in both app servers, you define the data source using the name
"jdbc/vehicles" and you can use the same persistence.xml file. Notice I
added a slash to the name to demonstrate that it is possible to do so.

-Dan
-- 
View this message in context: http://www.nabble.com/JPA-portability-with-JNDI-names-tp20680279p20686075.html
Sent from the java.net - glassfish users mailing list archive at Nabble.com.