[jboss-user] [JNDI/Naming/Network] - Configuring persistence unit and accessing EntityManager via
jsolderitsch
do-not-reply at jboss.com
Wed Nov 28 14:28:55 EST 2007
I posted a related query to the Netbeans sub-forum but I see very little activity there. And maybe I should be using the persistence sub-forum.
I have a project, originally developed in Netbeans 6 and deployed to Glassfish. I would like to deploy to JBoss 4.2.2 with as much of the code and configuration files as un-modified as possible.
I am using JAX-WS annotations for the Web Service declarations and also am using the Java Persistence API (JPA).
But I am not writing a EJB app -- just a web app, deploying with a .war file.
I am using MySQL and I believe I have the mysql-ds.xml file as it should be. There I declare the JNDI name:
<local-tx-datasource>
<jndi-name>mysql_remote</jndi-name>
My code relies on an EntityManager obtained via dependency injection like this:
@WebService()
@PersistenceContext(name = "persistence/LogicalName", unitName = "MissionStatusProjectPU2")
public class MissionStatusService {
EntityManager em;
public MissionStatusService() {
try {
Context ctx = (Context) new javax.naming.InitialContext().lookup("java:comp/env");
em = (EntityManager) ctx.lookup("persistence/LogicalName");
} catch (NamingException ex) {
ex.printStackTrace();
}
}
...
My persistence properties are declared in a very simple persistence.xml file like:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MissionStatusProjectPU2" transaction-type="JTA">
<jta-data-source>mysql_remote</jta-data-source>
</persistence-unit>
But in JBoss, I don't see how to further relate the MySQL data source (called mysql_remote) to an EntityManager.
The code above leads to a run-time error for JBoss:
17:44:54,134 ERROR [STDERR] javax.naming.NameNotFoundException: persistence not bound
17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
17:44:54,134 ERROR [STDERR] at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
17:44:54,135 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
17:44:54,135 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
17:44:54,135 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
17:44:54,135 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
17:44:54,135 ERROR [STDERR] at com.gestalt.afmstt.modernization.MissionStatusService.(MissionStatusService.java:35)
Line 35 is the ctx initialization line in the code fragment.
This suggests to me that I need some additional resource properties defined somewhere (jboss-web.xml or web.xml perhaps) to link the EntityManager to the datasource.
I confess I was spoiled by the config file automation that netbeans provides when dealing with glassfish.
Can anyone suggest whether my approach is even possible for JBoss 4.2.2 or if there are known issues preventing this?
If it is possible, what adjustments are needed in persistence.xml, jboss-web.xml and/or web.xml to allow me to overcome my current failure?
I can provide further code details upon request.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4108657#4108657
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4108657
More information about the jboss-user
mailing list