I'm not sure jays' info is correct.

You can get an EntityManager in a servlet as long as you look it up in jndi, just don't inject it using annotations or cache it in a field in your servlet.

I think the problem is that you are looking up java:comp/env/jdbc/ jBasicDB which is where the datasource is expected to be, not the EntityManager. Also in your geronimo plan you have a persistence- unit-ref specified but you want a persistence-context-ref since you are looking for an EntityManager, not an EntityManagerFactory.

I would suggest that you look up java:comp/env/jBasicDB in your servlet, and include this in your web.xml and then you won't need anything in your geronimo-web plan:

<persistence-context-ref>
<persistence-context-ref-name>jBasicDB</persistence-context-ref- name>
    <!-- I can't remember if anything else is required -->
</persistence-context-ref>

Also I strongly recommend including a non-jta-datasource in the persistence.xml, I haven't been able to get anything to work with derby without that. mysql may be more forgiving/less prone to weird deadlocks. Leave out all the connect-to-mysql parameters in persistence.xml and use the datasource.

You don't need a resource-ref anywhere if you aren't using the db directly from your servlet.

I assume you've found the jpa sample under testsuite/.....? It uses derby but does show a (usually) working configuration.

Hope this helps
david jencks

On Jun 15, 2007, at 12:20 PM, Jay D. McHugh wrote:

Peter,

I just realized - if you are doing resource_local access, you don't actually need the database pool.

OpenJPA will create connections as it needs them.

I would leave the database pool there though - it doesn't hurt anyone having it there.

Once you manage to get your app deployed, take a look in the jmx viewer of the admin console under J2EE Managed Objects/Persistence Unit to make sure it got deployed correctly. Also, when you deploy, there will be a message dumped to the log about a property being unrecognized - you can ignore it. It is an issue with OpenJPA that already has a JIRA filed for it.

Make sure you have the synchronizeMappings property set in your persistence.xml - otherwise the Geronimo default will kick in and try to recreate your tables and destroy all of your data.

If I have time to try fixing my app to use the jta datasource - I'll let you know what needs to change.


Jay

Jay D. McHugh wrote:
Hi Peter,

In a webapp, you are not allowed to directly get an EntityManager (they aren't threadsafe). Instead, you need to get an EntityManagerFactory.

I was originally not able to get access to a jta-datasource, I had to go with resource-local. I never went back to try accessing a database pool - so I'm going to tell you how I got things working in my current setup (using your values).

_persistence.xml

_<?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"; version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd";> <persistence-unit transaction-type="RESOURCE_LOCAL" name="jbasicDB"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</ provider>
       <class>nu.m4u.jbasic.control.jpa.Users</class>
       <exclude-unlisted-classes />
       <properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/jbasic"/> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="openjpa.ConnectionUserName" value="xxxxx"/> <property name="openjpa.ConnectionPassword" value="xxxxx"/> <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/> <property name="openjpa.jdbc.DBDictionary" value="mysql (SupportsSubselect=true)" />
           <property name="openjpa.Log" value="DefaultLevel=INFO" />
           <property name="openjpa.AutoDetach" value="close" />
           <property name="openjpa.DetachState" value="all" />
           <property name="openjpa.DataCache" value="false" />
           <property name="openjpa.Optimistic" value="true" />
           <property name="openjpa.Multithreaded" value="true" />
           <property name="openjpa.TransactionMode" value="local" />
<property name="openjpa.NontransactionalRead" value="true" />
           <property name="openjpa.RestoreState" value="all" />
<property name="openjpa.jdbc.SynchronizeMappings" value="false" />
       </properties>
   </persistence-unit>
</persistence>

_geronimo-web.xml:_

Remove all of the persistence unit info ->

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.2";
       xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1";
       xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1";
       xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1";>
<sys:environment>
  <sys:moduleId>
    <sys:groupId>jbasic</sys:groupId>
    <sys:artifactId>jbasic2</sys:artifactId>
    <sys:type>war</sys:type>
  </sys:moduleId>
  <sys:dependencies>
    <sys:dependency>
       <sys:groupId>console.dbpool</sys:groupId>
       <sys:artifactId>jbasicDB</sys:artifactId>
       <sys:version>1.0</sys:version>
       <sys:type>rar</sys:type>
    </sys:dependency>         <sys:dependency>
      <sys:groupId>mysql</sys:groupId>
      <sys:artifactId>mysql-connector-java</sys:artifactId>
      <sys:version>3.1.12</sys:version>
      <sys:type>jar</sys:type>
       </sys:dependency>
</sys:dependencies>
</sys:environment>
<context-root>/jbasic2</context-root>
</web-app>

_web.xml:_

Remove the peristence info.

_database pool plan:_

looks fine


Then, in order to get an EntityManagerFactory in your servlets use resource injection to create a class variable:

@PersistenceUnit(unitName="jbasicDB") private EntityManagerFactory emf;

And finally, wherever you need access to an EntityManager:

EntityManager em = emf.createEntityManager();

em.getTransaction().begin();

// do stuff

em.getTransaction().commit();

em.close();

If you are only reading from your database, you can leave off the 'em.getTransaction()' stuff. But if you are modifying data - you will need these otherwise, when you close your EntityManager instances all of your changes go away.

Hope this helps,


Jay

Peter Petersson wrote:
Hi

Need help on resolving the following configuration problem.
I am trying to find out how to get a geronimo database pool (mysql) used in a webapp, JPA managed in G v2.0 M6 (this is just a WAR not a EAR).

I end up stuck at deployment with the following error message.

13:42:42,291 ERROR [Deployer] Deployment failed due to
org.apache.geronimo.common.DeploymentException: At least one deployment problem: [org.apache.geronimo.common.DeploymentException: Could not resolve reference at deploy time for query ? name=jbasicDB#org.apache.geronimo.persistence.PersistenceUnitGBean. No GBean references found.] at org.apache.geronimo.persistence.builder.PersistenceUnitRefBuilder.bu ildNaming(PersistenceUnitRefBuilder.java:154) at org.apache.geronimo.persistence.builder.PersistenceUnitRefBuilder$ $FastClassByCGLIB$$9679ec9.invoke(<generated>)

I have looked around for G/JPA configuration information but the ones I have found aether deals with G:s derby db (examples working fine) or is a fully fledged EAR application which is a bit of an overkill as this app is meant to be used as a educational "stepping stone" app for some colleagues ( and me as it seems ;) ).

I'm trying to get hold of the entity manager with the following JNDI context lookup name "java:comp/env/jdbc/jbasicDB"

Here is relevant (or not so) parts of the current incarnation of the configuration:

persistency.xml (in webapp/WEB-INF)
------------------------------------------------
<persistence xmlns="http://java.sun.com/xml/ns/persistence";
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
            version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/ persistence http://java.sun.com/xml/ns/persistence/ persistence_1_0.xsd">

 <persistence-unit name="jbasicDB" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</ provider>
  <jta-data-source>jdbc/jbasicDB</jta-data-source>
  <class>nu.m4u.jbasic.control.jpa.Users</class>
  <exclude-unlisted-classes/>
   <properties>
<property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/> <property name="openjpa.ConnectionURL" value="jdbc:mysql:// localhost:3306/jbasic"/> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
     <property name="openjpa.ConnectionUserName" value="xxxxx"/>
     <property name="openjpa.ConnectionPassword" value="xxxxx"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
   </properties>    </persistence-unit>
</persistence>
------------------------------------------------

geronimo-web.xml
------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.2";
        xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1";
        xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1";
xmlns:sys="http://geronimo.apache.org/xml/ns/ deployment-1.1">
 <sys:environment>
   <sys:moduleId>
     <sys:groupId>jbasic</sys:groupId>
     <sys:artifactId>jbasic2</sys:artifactId>
     <sys:type>war</sys:type>
   </sys:moduleId>
   <sys:dependencies>
     <sys:dependency>
        <sys:groupId>console.dbpool</sys:groupId>
        <sys:artifactId>jbasicDB</sys:artifactId>
        <sys:version>1.0</sys:version>
        <sys:type>rar</sys:type>
     </sys:dependency>         <sys:dependency>
       <sys:groupId>mysql</sys:groupId>
       <sys:artifactId>mysql-connector-java</sys:artifactId>
       <sys:version>3.1.12</sys:version>
       <sys:type>jar</sys:type>        </sys:dependency>
     <!--        <sys:dependency>
        <sys:groupId>org.apache.geronimo.configs</sys:groupId>
        <sys:artifactId>openjpa</sys:artifactId>
        <sys:type>car</sys:type>
     </sys:dependency>
     -->            </sys:dependencies>       </sys:environment>

 <context-root>/jbasic2</context-root>

 <persistence-unit-ref>
<persistence-unit-ref-name>jdbc/jbasicDB</persistence-unit- ref-name>
    <persistence-unit-name>jbasicDB</persistence-unit-name>
 </persistence-unit-ref>

<!--        <sys:resource-ref>
    <sys:ref-name>jdbc/jbasicDB</sys:ref-name>
    <sys:resource-link>jbasicDB</sys:resource-link>
 </sys:resource-ref>
-->  </web-app>   ------------------------------------------------

web.xml
------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http:// java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <!-- Nothing much here yet -->    <!--         <resource-ref>
       <res-ref-name>jdbc/jbasicDB</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> --> </web-app> ------------------------------------------------

database pool plan
------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/ connector-1.2"> <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/ deployment-1.2">
       <dep:moduleId>
           <dep:groupId>console.dbpool</dep:groupId>
           <dep:artifactId>jbasicDB</dep:artifactId>
           <dep:version>1.0</dep:version>
           <dep:type>rar</dep:type>
       </dep:moduleId>
       <dep:dependencies>
           <dep:dependency>
               <dep:groupId>mysql</dep:groupId>
               <dep:artifactId>mysql-connector-java</dep:artifactId>
               <dep:version>3.1.12</dep:version>
               <dep:type>jar</dep:type>
           </dep:dependency>
       </dep:dependencies>
   </dep:environment>
   <resourceadapter>
       <outbound-resourceadapter>
           <connection-definition>
<connectionfactory-interface>javax.sql.DataSource</ connectionfactory-interface>
               <connectiondefinition-instance>
                   <name>jbasicDB</name>
<config-property-setting name="Password">xxxx</ config-property-setting> <config-property-setting name="Driver">com.mysql.jdbc.Driver</config-property-setting> <config-property-setting name="UserName">xxxx</ config-property-setting> <config-property-setting name="ConnectionURL">jdbc:mysql://localhost:3306/jbasic</config- property-setting>
                   <connectionmanager>
                       <local-transaction/>
                       <single-pool>
                           <max-size>10</max-size>
                           <min-size>0</min-size>
                           <match-one/>
                       </single-pool>
                   </connectionmanager>
               </connectiondefinition-instance>
           </connection-definition>
       </outbound-resourceadapter>
   </resourceadapter>
</connector>
------------------------------------------------

Anny help is appreciated !
/Peter Petersson







Reply via email to