Explicit Transaction Demarcation on the Client - Hello World
molbrich Jul 17, 2004 8:58 AMHello i am a newby with EJBTransactions,
i just create some Hello World examples on how EJB works.
But after reading a lot and searching the WEB for two days i see no reason why that should not work. Imo this should be the standard way for Client managed explicit Transactions. Isnt it?
I already solved two non standard problems: 1st setting the jndi Props by System.setProperty() and 2nd the non standard JNDI name of the UserTransaction is "java:UserTransaction" instead of the spec. defined standard "java:comp/UserTransaction".
But it always throws this Exception in the line of home.create():
javax.transaction.TransactionRequiredException: Transaction Required
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:316)
at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:74)
Imo the UserTransaction should be passed to the Bean and used.
The transaction-attribute = mandatory forced that.
// ######### The Client ##################
public class ExplicitUserTransactionClient {
public static void main(String argv[]) throws Exception {
// solves JBoss Bug: UT factory lookup failed
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
System.setProperty(Context.PROVIDER_URL, "http://vwplan.wob.vw.vwg:8092/invoker/JNDIFactory");
Context context = new InitialContext();
// Lookup non standard Name of Jboss UserTransaction and start it
UserTransaction ta = (UserTransaction)context.lookup("UserTransaction");
ta.begin();
// use an EJBean, TA should be propagated to it
Object ref = context.lookup("java:/ejbtest/ejb/EXTABean");
EXTABeanHome home = (EXTABeanHome)PortableRemoteObject.narrow(ref, EXTABeanHome.class);
EXTABean bean = (EXTABean) home.create();
// End userTransaktion (on the Client)
ta.commit();
context.close();
}
}
// ######### The Bean ##################
/**
* @ejb.bean
* name="EXTABean"
* view-type="remote"
* type="Stateful"
* jndi-name="ejbtest/ejb/EXTABean"
* @jboss.container-configuration name="HTTP Stateful SessionBean"
*
* @ejb.bean transaction-type="Container"
* @ejb.transaction type="mandatory"
*/
public class EXTABean implements SessionBean {
public EXTABean() {
super();
}
public void ejbCreate() throws CreateException {}
public void ejbActivate() throws EJBException, RemoteException {}
public void ejbPassivate() throws EJBException,RemoteException {}
public void ejbRemove() throws EJBException, RemoteException {}
public void setSessionContext(SessionContext arg0)
throws EJBException, RemoteException {}
}
// ######### The XDoclet generated ejb-jar.xml (partially) ####
<!-- Session Beans -->
<![CDATA[]]>
<ejb-name>EXTABean</ejb-name>
ejbtest.interfaces.EXTABeanHome
ejbtest.interfaces.EXTABean
<ejb-class>ejbtest.ejb.EXTABean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
<!-- transactions -->
<container-transaction >
<ejb-name>EXTABean</ejb-name>
<method-name>*</method-name>
<trans-attribute>mandatory</trans-attribute>
</container-transaction>
Thanks for your help.
Markus