The Source for Java Technology Collaboration
Webmaster Alert: Posting to Jive Forums is currently not working. Estimated time for fix is unknown.

Home » java.net Forums » GlassFish » GlassFish

Thread: need help to configure persistence.xml with tomcat datasource

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 14 - Last Post: Jan 2, 2007 8:48 AM by: tdahlke
gkatz

Posts: 10
need help to configure persistence.xml with tomcat datasource
Posted: Apr 28, 2006 12:26 PM
  Click to reply to this thread Reply

hi all;
i cant seem to figure out how to configure glassfish jpa to use tomcat datasource. (regular configuration through properties works fine but i already have a tomcat DS that i want to use). i would apreciate any help on this.
thanks in advance.
my persistence.xml is as follows:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="xwave">
<non-jta-data-source>waves/jdbc/WavesDB</non-jta-data-source>
<properties>
<property name="toplink.logging.level" value="DEBUG"/>
</properties>
</persistence-unit>
</persistence>
the exception i get:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.4 (Build 060412)): or
acle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: The url cannot be nullError Code: 0
at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(D
atabaseException.java:289)
at oracle.toplink.essentials.jndi.JNDIConnector.connect(JNDIConnector.ja
va:135)
at oracle.toplink.essentials.sessions.DatasourceLogin.connectToDatasourc
e(DatasourceLogin.java:167)
at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.login
AndDetectDatasource(DatabaseSessionImpl.java:537)
at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.login

ss141213

Posts: 500
Re: need help to configure persistence.xml with tomcat datasource
Posted: Apr 29, 2006 1:29 AM   in response to: gkatz
  Click to reply to this thread Reply

Tomcat is considered as Java SE environment. In Java SE, datasource is not supported. These are the exact wordings in the spec (section #6.2.1.5):

In Java SE environments, these elements may be used or the data source information may be specified
by other means—depending upon the requirements of the provider.


So you have to specify database properties using <properties> as shown below. More over, there are a few important ways persistence.xml varies between Java EE & Java SE environment, e.g. you have to specify provider name using <provider> element, list all managed classes using <class> element and only RESOURCE_LOCAL transaction type is supported.

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="xwave" transaction-type="RESOURCE_LOCAL">
        <provider> oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
        <class>entity1</class>
        <class>entity2</class>
        <properties>
            <property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/sun-appserv-samples"/>
            <property name="toplink.jdbc.user" value="APP"/>
            <property name="toplink.jdbc.password" value="APP"/>
        </properties>
    </persistence-unit>
</persistence>


Hope that helps,
Sahoo

gkatz

Posts: 10
Re: need help to configure persistence.xml with tomcat datasource
Posted: Apr 29, 2006 2:43 AM   in response to: ss141213
  Click to reply to this thread Reply

thanks for the reply.
my environment already uses a datasource. for example, i have a database realm for authentication etc...
i find it hard to believe that the persistence spec people did not think that most people would rather use the existing datasources they have and just point the JPA factory to the existing datasource.

using the properties will cause another datsource over the DB to be created which i dont really want.

i thought that non-jta-datasource was what i needed but maybe i was wrong.
r u 100% sure that there is no way to use JPA with an exising tomcat datasource in tomcat? (without a full J2EE container)

thanks in advance

mvatkina

Posts: 764
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 1, 2006 6:40 PM   in response to: gkatz
  Click to reply to this thread Reply

I suggest you to file a bug or an enhancement in entity-persistence subcategory.

thanks,
-marina

ss141213

Posts: 500
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 2, 2006 11:31 AM   in response to: gkatz
  Click to reply to this thread Reply

I just spoke to an engineer who understands Tomcat and its road map better than me. Requiring user to specify those JDBC connection properties as opposed to using data-source element in persistence.xml is currently a limitation in Tomcat which does not yet support Servlet 2.5 spec. When Tomcat will implement Servlet 2.5 spec, you can use Java Persistence API just the way you use it in Java EE container.

Sahoo

vispper

Posts: 1
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 10, 2006 9:47 AM   in response to: ss141213
  Click to reply to this thread Reply

hi, how can i use toplink in a tomcat's web app

i put persistence.xml in WEB-INF/classes/META-INF and add JVM arg '-javaagent:xxx' in catalina.bat, but it won't works

thanks

ss141213

Posts: 500
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 10, 2006 11:01 AM   in response to: vispper
  Click to reply to this thread Reply

See if the following blog helps?

http://blogs.sun.com/roller/page/pblaha?entry=using_toplink_persistence_ri_in

gyorke

Posts: 208
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 25, 2006 8:27 AM   in response to: vispper
  Click to reply to this thread Reply

vispper,
Check out the example and tutorial at otn.oracle.com/jpa These have been sucessfully run in Tomcat.

When deploying in Tomcat the '-javaagent' option will not be usefull but is also not required to run TopLink. Without '-javaagent' the only functionality missing will be lazy loading of OneToOne relationships which should have no functional impact on your application.
--Gordon

gyorke

Posts: 208
Re: need help to configure persistence.xml with tomcat datasource
Posted: May 25, 2006 8:23 AM   in response to: gkatz
  Click to reply to this thread Reply

gkatz,
As of v2_build_02 (or perhaps v2_build_03) TopLink is able to access and use a non-jta datasource in Tomcat. Your application as configured should work.
--Gordon

gkatz

Posts: 10
Re: need help to configure persistence.xml with tomcat datasource
Posted: Aug 16, 2006 1:55 AM   in response to: gyorke
  Click to reply to this thread Reply

hi;
i am using v2_b12 and still cant get tomcat to work with a non-jta-datasource.

my persistence.xml is:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="xwave">
<non-jta-data-source>waves/jdbc/WavesDB</non-jta-data-source>
<properties>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>

for this configuration i get a name not bound exception as follows (even though the name is bound cause i access it from my regular java code in tomcat with no problems):
Exception Description: Cannot acquire data source [waves/jdbc/WavesDB].
Internal Exception: javax.naming.NameNotFoundException: Name waves is not bound in this Context
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:240)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:84)
...



i tried adding the 'java:comp/env/ prefix to my non-jta-datasource name but i get the following error:
Caused by: javax.naming.NamingException: This context must be accessed throught a java: URL
at org.apache.naming.SelectorContext.parseName(SelectorContext.java:685)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:120)
at javax.naming.InitialContext.lookup(InitialContext.java:355)
at oracle.toplink.essentials.jndi.JNDIConnector.connect(JNDIConnector.java:114)
... 41 more



i would appreciate any help i can get here. is this a bug? or am i doing something wrong.
thanks.

ailitche

Posts: 20
Re: need help to configure persistence.xml with tomcat datasource
Posted: Aug 18, 2006 8:15 AM   in response to: gkatz
  Click to reply to this thread Reply

Try using session customizer to investigate the problem.
Add to persistence.xml:
<property name="toplink.session.customizer" value="MySessionCustomizer"/>

Add to the deployment a new class
public class MySessionCustomizer implements oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer {
  public void customize(Session session) throws Exception{
    oracle.toplink.essentials.jndi.JNDIConnector connector = 
        (oracle.toplink.essentials.jndi.JNDIConnector)session.getLogin().getConnector();
    ...
  }    
}

customize method is called before connecting the session, it allows you to experiment with the connector.

Obtaining the datasource directly from jndi and setting it into the connector should always work:
  java.sql.Datasource dataSource = *obtained from jndi*
  connector.setDataSource(dataSource);


If this approach works, to investigate what went wrong in TopLink's attempt to find the datasource:

Wrong context?
  connector.setContext(correctContext);

Wrong type used for lookup?
  connector.setLookupType(JNDIConnector.STRING_LOOKUP);
  /*or*/
  connector.setLookupType(JNDIConnector.COMPOUND_NAME_LOOKUP);


gkatz

Posts: 10
Re: need help to configure persistence.xml with tomcat datasource
Posted: Aug 20, 2006 5:28 AM   in response to: ailitche
  Click to reply to this thread Reply

thanks 4 the help. here are the results:
using your suggestion i used the session customizer class and was successfull in loading the datasource. i then, as you sugested started the tests.
when the non-jta-datasource was set to "java:comp/env/waves/jdbc/WavesDB"
i was able to get JPA to work only by using connector.setLookupType(JNDIConnector.STRING_LOOKUP)

when the non-jta-datasource was set to "waves/jdbc/WavesDB" i could not get ti to work anyway.

so why doesn't it work out of the box for the 'java:comp/env/...'? is this a bug with topink?

thanks again

ailitche

Posts: 20
Re: need help to configure persistence.xml with tomcat datasource
Posted: Aug 22, 2006 7:56 AM   in response to: gkatz
  Click to reply to this thread Reply

It doesn't work out of the box, because JNDI lookup that works with Oc4j, WAS, WLS application servers
dataSource = (DataSource)getContext().lookup(new CompositeName(name));

apparently doesn't work on tomcat.

JNDIConnector.STRING_LOOKUP causes TopLink to lookup using a String name:
dataSource = (DataSource)getContext().lookup(name);

which I assume is exactly what you do when you lookup directly.

Why 'java:comp/env/' prefix should be specified in tomcat jndi datasource lookup I really don't know (could it be because tomcat is not an app. server?)
Google search for tomcat datasource brought the following link: http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html#Database%20Connection%20Pool%20(DBCP)%20Configurations that contains at least two lookup examples.

The good news is that you have a simple workaround.

gkatz

Posts: 10
Re: need help to configure persistence.xml with tomcat datasource
Posted: Oct 1, 2006 12:08 PM   in response to: ailitche
  Click to reply to this thread Reply

ok;
so after all the help i got here and many problems i had in production (see this thread start and http://forums.java.net/jive/thread.jspa?forumID=56&threadID=18743&messageID=158392#158392) i have swiched to the non-jta configuration so i can fully control pool properties (such as a test select statement) and utilize my existing datasource.
here is the configuration worked for me:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="xwave">
<non-jta-data-source>java:comp/env/jdbc/MyDB</non-jta-data-source>
<class>your.domain.class.list.here</class>
<properties>
<property name="toplink.session.customizer" value="com.company.application.JPAToplinkSessionCustomizationUtil"/>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>


here is the class JPAToplinkSessionCustomizationUtil:
public class JPAToplinkSessionCustomizationUtil implements oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer{
public void customize(Session session) throws Exception{
JNDIConnector connector = (JNDIConnector)session.getLogin().getConnector();
connector.setLookupType(JNDIConnector.STRING_LOOKUP);
}
}

my tomcat resource definitiion:
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="30000"
removeAbandoned="true" removeAbandonedTimeout="60"
logAbandoned="true" username="usr" password="pass"
driverClassName="com.mysql.jdbc.Driver" validationQuery="SELECT 1" testOnBorrow="true"
url="jdbc:mysql://localhost/MyDB?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&"/>

note that i read in mysql docs that using autoReconnect is not promoted...

thanks for the help, hope someone will get som value from this.

tdahlke

Posts: 1
Re: need help to configure persistence.xml with tomcat datasource
Posted: Jan 2, 2007 8:48 AM   in response to: gkatz
  Click to reply to this thread Reply

I was having the exact same problem, and your solution worked! Any word on whether this bug (it sounds like a bug) will be fixed?




 XML java.net RSS