Hi,
I am trying to deploy an example using EJB3 and JPA. It's an example from a
JPA + J2SE tutorial.
I am trying to make it work with EJB3.
I am using eclipse ganymede, postgresql 8.0.15 and glassfish V2.
Here is my code :
For the interface :
package logic;
import javax.ejb.Remote;
import persistence.Cat;
@Remote
public interface savingCats {
public void saveMyCat(Cat myCat);
public String sayHello(String myString);
}
For the bean implementing that interface :
package logic;
import persistence.Cat;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class savingCatsBean implements savingCats {
@PersistenceContext(unitName="ejb3_toplink_quickstart")
private EntityManager em;
@Override
public void saveMyCat(Cat myCat) {
try{
em.getTransaction().begin();
em.persist(myCat);
em.getTransaction().commit();
}
catch(Exception e){
em.getTransaction().rollback();
} finally {
em.close();
}
}
@Override
public String sayHello(String myString){
return "Hello " + myString;
}
}
For the entity :
package persistence;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
@Table(name = "CAT")
public class Cat implements Serializable{
private String myid;
private String myname;
private char mysex;
private float myweight;
public Cat() {
}
public Cat(String id, String name, char sex, float weight) {
myid = id;
myname = name;
mysex = sex;
myweight = weight;
}
@Id
public String getId() {
return myid;
}
public void setId(String id) {
myid = id;
}
public String getName() {
return myname;
}
public void setName(String name) {
myname = name;
}
public char getSex() {
return mysex;
}
public void setSex(char sex) {
mysex = sex;
}
public float getWeight() {
return myweight;
}
public void setWeight(float weight) {
myweight = weight;
}
}
In my ejb project, I set up JPA like that :
Platform : Generic
Override default schema from connection : yes (public)
Persistent class management : discover annotated class automatically
My connection is setup like that :
Database : animalerie
URL : jdbc:postgresql://localhost:5432/animalerie
Username : postgres
Password : postgres
When I test my connection, the ping work well.
After that I create a database with psql : animalerie.
And I set a connection pool in glassfish :
DataSourceclassname : org.postgresql.ds.PGConnectionPoolDataSource
Ressource type : javax.sql.ConnectionPoolDataSource
user : postgres
password : postgres
servername : localhost
portnumber : 5432
JDBC30DataSource : true
DatabaseName : animalerie
Then I set up a jdbc ressources : jdbc/animalerie with the connection pool i
set up before.
I have downloaded the jdbc driver in the ext directory :
lib/ext/postgresql-8.2-508.jdbc3.jar
And added it in the libraries list needed by the ejb project.
Here is the code for my client. I use JNDI
import javax.naming.InitialContext;
import persistence.Cat;
import logic.*;
public class saveHer {
/**
* @param args
*/
public static void main(String[] args) {
try{
InitialContext ctx = new InitialContext();
savingCats sc = (savingCats)ctx.lookup("logic.savingCats");
Cat princess = new Cat("32", "Princess", 'F', 7.4f);
sc.saveMyCat(princess);
// System.out.println(sc.sayHello("boy"));
}
catch (Exception e){
e.printStackTrace();
} // TODO Auto-generated method stub
}
}
I have added the toplink-essentials-agent to my client project and the
postgresql jar too.....
The EJB deployed works since I can call the sayHello method and it return
the string I expected.
But when I try to persist a cat, I have this exception :
javax.ejb.EJBException: nested exception is: java.rmi.ServerException:
RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: null; nested exception is:
java.lang.IllegalStateException
java.rmi.ServerException: RemoteException occurred in server thread; nested
exception is:
java.rmi.RemoteException: null; nested exception is:
java.lang.IllegalStateException
at
com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:243)
at
com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:205)
at
com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152)
at
com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(BCELStubBase.java:225)
at
logic.__savingCats_Remote_DynamicStub.saveMyCat(logic/__savingCats_Remote_DynamicStub.java)
at logic._savingCats_Wrapper.saveMyCat(logic/_savingCats_Wrapper.java)
at saveHer.main(saveHer.java:17)
Caused by: java.rmi.RemoteException: null; nested exception is:
java.lang.IllegalStateException
at
com.sun.enterprise.iiop.POAProtocolMgr.mapException(POAProtocolMgr.java:251)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1386)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1316)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:210)
at
com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:77)
at $Proxy40.saveMyCat(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:154)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:687)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:227)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1846)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1706)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:1088)
at
com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:223)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:806)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:563)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2567)
at
com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:555)
Caused by: java.lang.IllegalStateException
at
com.sun.enterprise.util.EntityManagerWrapper.close(EntityManagerWrapper.java:546)
at logic.savingCatsBean.saveMyCat(savingCatsBean.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1067)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:176)
at
com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2895)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:3986)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
... 17 more
javax.ejb.EJBException: nested exception is: java.rmi.ServerException:
RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: null; nested exception is:
java.lang.IllegalStateException
at logic._savingCats_Wrapper.saveMyCat(logic/_savingCats_Wrapper.java)
at saveHer.main(saveHer.java:17)
I do not know what is not working. Please help me. I think that I Glassfish
can connect to the database since before deploying the ejb, i drop all
database. And after the deployment the table "cat" has been created.
Thank you everyone.
--
View this message in context: http://www.nabble.com/Glassfish--Toplink--Postgresql-can%27t-persist-tp18821973p18821973.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.