|
Replies:
14
-
Last Post:
Jan 2, 2007 8:48 AM
by: tdahlke
|
|
|
|
|
|
|
need help to configure persistence.xml with tomcat datasource
Posted:
Apr 28, 2006 12:26 PM
|
|
|
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
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Apr 29, 2006 1:29 AM
in response to: gkatz
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Apr 29, 2006 2:43 AM
in response to: ss141213
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
May 1, 2006 6:40 PM
in response to: gkatz
|
|
|
I suggest you to file a bug or an enhancement in entity-persistence subcategory.
thanks, -marina
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
May 2, 2006 11:31 AM
in response to: gkatz
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
May 10, 2006 9:47 AM
in response to: ss141213
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
May 25, 2006 8:27 AM
in response to: vispper
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
May 25, 2006 8:23 AM
in response to: gkatz
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Aug 16, 2006 1:55 AM
in response to: gyorke
|
|
|
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.
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Aug 18, 2006 8:15 AM
in response to: gkatz
|
|
|
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);
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Aug 20, 2006 5:28 AM
in response to: ailitche
|
|
|
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
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Aug 22, 2006 7:56 AM
in response to: gkatz
|
|
|
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.
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Oct 1, 2006 12:08 PM
in response to: ailitche
|
|
|
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.
|
|
|
|
|
|
|
|
Re: need help to configure persistence.xml with tomcat datasource
Posted:
Jan 2, 2007 8:48 AM
in response to: gkatz
|
|
|
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?
|
|
|
|
|