RE: Transaction across several different methods in Session beans
Sorry, my begin method is: public void begin() { try { ejbCtx.getUserTransaction().begin(); } catch(Exception e) { e.printStackTrace(); } } I make this mistake becose I before try do this using Context ctx = new InitialContext(); utx = (UserTransaction)ctx.lookup("java:comp/UserTransaction"); but I catch the same exception. Thanks. > -Original Message- > From: Scott Stirling [SMTP:[EMAIL PROTECTED]] > Sent: Thursday, December 14, 2000 3:03 AM > To: Orion-Interest > Subject: RE: Transaction across several different methods in Session > beans > > Maybe you're calling begin() and commit() on two different > UserTransactions. > Does the line where you assign the return value of UserTransaction.begin() > to utx work? That looks weird (shorthand coding style), but if it works I > guess it's OK. The type of utx isn't declared anywhere in the code you > posted. > > Check these lines: > > public void begin() > { > try > { > utx = ejbCtx.getUserTransaction().begin(); > } > > > and: > > public void commit() > { > try > { > ejbCtx.getUserTransaction().commit(); > } > > Scott Stirling > West Newton, MA > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED]]On Behalf Of Lopez Esteban > Sent: Wednesday, December 13, 2000 6:14 AM > To: Orion-Interest > Subject: Transaction across several different methods in Session beans > > > Hi all, > I'm trying to use UserTransactions in a stateful session bean to associate > itself with a transaction across several diferent methods, but it does not > work. My code is: > > Stateful Session Bean: > public class TransactionBean implements SessionBean > { > private SessionContext ejbCtx; >private Entity entityRole = null; > > public void setSessionContext(SessionContext context) > throws RemoteException, EJBException > { > ejbCtx = context; > } > > public void ejbActivate() throws RemoteException, EJBException > { > } > > public void ejbPassivate() throws RemoteException, EJBException > { > } > > public void ejbRemove() throws RemoteException, EJBException > { > } > > public void ejbCreate() throws CreateException, EJBException, > RemoteException > { > } > > > public void createEntity(String id) > { > try > { > Context context = new InitialContext(); > EntityHome entityHome = > (EntityHome)PortableRemoteObject.narrow(context.lookup("java:comp/env/enti > ty > "), > EntityHome.class); > ejbEntity = entityHome.create(id); > } > catch (Exception ne) > { > ejbEntity = null; > ne.printStackTrace(); > } > } > > public void setDescription(String description) throws RemoteException > { > if (ejbEntity == null) > { > throw( > new RemoteException("Entity is null, use createEntity() first")); > } > try > { > ejbEntity.setDescription(description); > } > catch (RemoteException re) > { > re.printStackTrace(); > } > } > > public void begin() > { > try > { > utx = ejbCtx.getUserTransaction().begin(); > } > catch(Exception e) > { > e.printStackTrace(); > } > } > > public void commit() > { > try > { > ejbCtx.getUserTransaction().commit(); > } > catch(Exception e) > { > e.printStackTrace(); > } > > } > > public void rollback() > { > try > { > ejbCtx.getUserTransaction().rollback(); > } > catch(Exception e) > { > e.printStackTrace(); > } > } > } > > and my client is: > > public class TransactionTest > { > public static void main(String[] args) > { > try > { > Context context = new InitialContext(); > TransactionHome home = > (TransactionHome)PortableRemoteObject.narrow(context.lookup("java:comp/env > /t > ransaction"), > TransactionHome.class); > Transaction tx = home.create(); > tx.begin(); > tx.createEntity("tx entity"); > tx.setDescription("tx description"); > tx.commit(); > } > catch (Exception e) > { > e.printStackTrace(); > } > } > } > > I'm using Orion 1.4.4 and Windows 98. I set > Bean in the ejb-jar.xml of
RE: Transaction across several different methods in Session beans
Maybe you're calling begin() and commit() on two different UserTransactions. Does the line where you assign the return value of UserTransaction.begin() to utx work? That looks weird (shorthand coding style), but if it works I guess it's OK. The type of utx isn't declared anywhere in the code you posted. Check these lines: public void begin() { try { utx = ejbCtx.getUserTransaction().begin(); } and: public void commit() { try { ejbCtx.getUserTransaction().commit(); } Scott Stirling West Newton, MA -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Lopez Esteban Sent: Wednesday, December 13, 2000 6:14 AM To: Orion-Interest Subject: Transaction across several different methods in Session beans Hi all, I'm trying to use UserTransactions in a stateful session bean to associate itself with a transaction across several diferent methods, but it does not work. My code is: Stateful Session Bean: public class TransactionBean implements SessionBean { private SessionContext ejbCtx; private Entity entityRole = null; public void setSessionContext(SessionContext context) throws RemoteException, EJBException { ejbCtx = context; } public void ejbActivate() throws RemoteException, EJBException { } public void ejbPassivate() throws RemoteException, EJBException { } public void ejbRemove() throws RemoteException, EJBException { } public void ejbCreate() throws CreateException, EJBException, RemoteException { } public void createEntity(String id) { try { Context context = new InitialContext(); EntityHome entityHome = (EntityHome)PortableRemoteObject.narrow(context.lookup("java:comp/env/entity "), EntityHome.class); ejbEntity = entityHome.create(id); } catch (Exception ne) { ejbEntity = null; ne.printStackTrace(); } } public void setDescription(String description) throws RemoteException { if (ejbEntity == null) { throw( new RemoteException("Entity is null, use createEntity() first")); } try { ejbEntity.setDescription(description); } catch (RemoteException re) { re.printStackTrace(); } } public void begin() { try { utx = ejbCtx.getUserTransaction().begin(); } catch(Exception e) { e.printStackTrace(); } } public void commit() { try { ejbCtx.getUserTransaction().commit(); } catch(Exception e) { e.printStackTrace(); } } public void rollback() { try { ejbCtx.getUserTransaction().rollback(); } catch(Exception e) { e.printStackTrace(); } } } and my client is: public class TransactionTest { public static void main(String[] args) { try { Context context = new InitialContext(); TransactionHome home = (TransactionHome)PortableRemoteObject.narrow(context.lookup("java:comp/env/t ransaction"), TransactionHome.class); Transaction tx = home.create(); tx.begin(); tx.createEntity("tx entity"); tx.setDescription("tx description"); tx.commit(); } catch (Exception e) { e.printStackTrace(); } } } I'm using Orion 1.4.4 and Windows 98. I set Bean in the ejb-jar.xml of Transaction Bean and entity * Required for Entity bean. When I try to use this I catch the folow exception: java.lang.IllegalArgumentException: No active Transaction at com.evermind.server.ApplicationServerTransactionManager.commit(JAX) at transaction.TransactionBean.commit(TransactionBean.java:127) at Transaction_StatefulSessionBeanWrapper0.commit(Transaction_StatefulSessionBe anWrapper0.java:190) at java.lang.reflect.Method.invoke(Native Method) at com.evermind.server.rmi.bf.do(JAX) at com.evermind.util.f.run(JAX) Any ideas. Thanks, Esteban
RE: Transaction across several different methods in Session beans
hi esteban... have you tried holding on to a UserTransacion reference? if you don't, the reference to the UserTransaction is gc'ed after the try clause of the methods that look it up... If you solve it, please post the list saludos Juan Pablo -Original Message- From: Lopez Esteban [mailto:[EMAIL PROTECTED]] Sent: MiƩrcoles, 13 de Diciembre de 2000 8:14 To: Orion-Interest Subject: Transaction across several different methods in Session beans Hi all, I'm trying to use UserTransactions in a stateful session bean to associate itself with a transaction across several diferent methods, but it does not work. My code is: Stateful Session Bean: public class TransactionBean implements SessionBean { private SessionContext ejbCtx; private Entity entityRole = null; public void setSessionContext(SessionContext context) throws RemoteException, EJBException { ejbCtx = context; } public void ejbActivate() throws RemoteException, EJBException { } public void ejbPassivate() throws RemoteException, EJBException { } public void ejbRemove() throws RemoteException, EJBException { } public void ejbCreate() throws CreateException, EJBException, RemoteException { } public void createEntity(String id) { try { Context context = new InitialContext(); EntityHome entityHome = (EntityHome)PortableRemoteObject.narrow(context.lookup("java:comp/env/entity "), EntityHome.class); ejbEntity = entityHome.create(id); } catch (Exception ne) { ejbEntity = null; ne.printStackTrace(); } } public void setDescription(String description) throws RemoteException { if (ejbEntity == null) { throw( new RemoteException("Entity is null, use createEntity() first")); } try { ejbEntity.setDescription(description); } catch (RemoteException re) { re.printStackTrace(); } } public void begin() { try { utx = ejbCtx.getUserTransaction().begin(); } catch(Exception e) { e.printStackTrace(); } } public void commit() { try { ejbCtx.getUserTransaction().commit(); } catch(Exception e) { e.printStackTrace(); } } public void rollback() { try { ejbCtx.getUserTransaction().rollback(); } catch(Exception e) { e.printStackTrace(); } } } and my client is: public class TransactionTest { public static void main(String[] args) { try { Context context = new InitialContext(); TransactionHome home = (TransactionHome)PortableRemoteObject.narrow(context.lookup("java:comp/env/t ransaction"), TransactionHome.class); Transaction tx = home.create(); tx.begin(); tx.createEntity("tx entity"); tx.setDescription("tx description"); tx.commit(); } catch (Exception e) { e.printStackTrace(); } } } I'm using Orion 1.4.4 and Windows 98. I set Bean in the ejb-jar.xml of Transaction Bean and entity * Required for Entity bean. When I try to use this I catch the folow exception: java.lang.IllegalArgumentException: No active Transaction at com.evermind.server.ApplicationServerTransactionManager.commit(JAX) at transaction.TransactionBean.commit(TransactionBean.java:127) at Transaction_StatefulSessionBeanWrapper0.commit(Transaction_StatefulSessionBe anWrapper0.java:190) at java.lang.reflect.Method.invoke(Native Method) at com.evermind.server.rmi.bf.do(JAX) at com.evermind.util.f.run(JAX) Any ideas. Thanks, Esteban