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.buildNaming(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