14 Replies Latest reply on Oct 9, 2005 3:07 AM by jaap

    Simple BMP method throws TransactionRolledbackException

    Bora Bora Newbie

      Hi people,

      I am new to EJB's and I couldn't find a solution to this: I deployed a BMP and got its remote interface through findByPrimaryKey. But when a method is called on the bean I get TransactionRolledbackException.

      Here is the code:

      ***
      UserHome userHome = null;
      InitialContext ctx = new InitialContext();
      Object objref = ctx.lookup("ejb/test/MyTestBmpBean");
      userHome = (UserHome) PortableRemoteObject.narrow(objref,
       UserHome.class);
      ***
      User beanRemote;
      beanRemote = userHome.findByPrimaryKey("somename");
      String str = null;
      if(beanRemote != null){
       // FOR THIS LINE THE EXCEPTION IS THROWN
       str = beanRemote.getPassword();
      }
      
      


      I can include the server log if need be...

      Thanks in advance.

        • 1. Re: Simple BMP method throws TransactionRolledbackException
          Darran Lofthouse Master

          We don't need the whole server log but we do need the exception with it's message and stack trace.

          • 2. Re: Simple BMP method throws TransactionRolledbackException
            Bora Bora Newbie

            Here is the stack trace (not all):
            2005-10-03 16:23:49,437 ERROR [org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackException in method: public abstract java.lang.String com.ejb.User.getPassword() throws java.rmi.RemoteException, causedBy:
            org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=spider/15, BranchQual=, localId=15] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.ejb.EJBException: null; CausedByException is:
            null)
            at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:344)
            at org.jboss.ejb.plugins.TxInterceptorCMT.endTransaction(TxInterceptorCMT.java:486)
            at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:346)
            at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:166)
            at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:139)
            at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:192)
            at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
            at org.jboss.ejb.EntityContainer.internalInvoke(EntityContainer.java:514)
            at org.jboss.ejb.Container.invoke(Container.java:873)
            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:585)
            at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
            at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
            at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
            at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
            at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
            at org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:155)
            at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:104)
            at org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:179)
            at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:165)
            at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
            at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:55)
            at org.jboss.proxy.ejb.EntityInterceptor.invoke(EntityInterceptor.java:97)
            at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:86)
            at $Proxy52.getPassword(Unknown Source)
            at com.client.BmpEntityTestServlet.doGet(BmpEntityTestServlet.java:71)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
            at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
            at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
            at java.lang.Thread.run(Thread.java:595)
            Caused by: javax.ejb.EJBException: null; CausedByException is:
            null
            at org.jboss.ejb.plugins.BMPPersistenceManager.invokeEjbStore(BMPPersistenceManager.java:504)
            at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeEjbStore(CachedConnectionInterceptor.java:412)
            at org.jboss.ejb.EntityContainer.invokeEjbStore(EntityContainer.java:729)
            at org.jboss.ejb.GlobalTxEntityMap$2.invokeEjbStore(GlobalTxEntityMap.java:117)
            at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.synchronize(GlobalTxEntityMap.java:239)
            at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.beforeCompletion(GlobalTxEntityMap.java:302)
            at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1384)
            at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1076)
            at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:296)
            ... 48 more

            • 3. Re: Simple BMP method throws TransactionRolledbackException
              Darran Lofthouse Master

              Ok the problem is that your client code is not running within a transaction, there are two main options around this.

              1 - Move the code that accesses the bean to a session bean and call a method on the session bean from the web appliction. The call to the session bean will automatically run within a single transaction unless you configure it otherwise.

              2 - In your servled lookup a javax.transaction.UserTransaction object from 'java:comp/UserTransaction and start a transaction before operating on the entity bean and at the end either commit or rollback the transaction.

              • 4. Re: Simple BMP method throws TransactionRolledbackException
                Bora Bora Newbie

                Hi darranl,

                When I first read your reply I thought it was spot on. I still think so but perhaps I am doing something wrong.

                Yes what I want to do is the thing you wrote in the 2nd option. I want to do some calls and operations on my BMP entity beans without using a session bean.

                Here is my new code after your suggestion and the stack trace follows. Thanks a lot.

                UserHome userHome = null;
                InitialContext ctx = new InitialContext();
                Object objref = ctx.lookup("ejb/test/MyTestBmpBean");
                userHome = (UserHome) PortableRemoteObject.narrow(objref,
                 UserHome.class);
                ***
                UserTransaction transaction = (UserTransaction)
                 ctx.lookup("java:comp/UserTransaction");
                try{
                 User beanRemote = null;
                 transaction.begin();
                 beanRemote = userHome.create("erbora01", "x", "x", "x", "x");
                 transaction.commit(); // LINE 74
                catch () {
                 // rollback etc
                 transaction.rollBack();// LINE 83
                }
                


                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.connectionmanager.IdleRemover] internalRegisterPool: registering pool with interval 300000 old interval: 9223372036854775807
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.connectionmanager.IdleRemover] internalRegisterPool: about to notify thread: old next: 1128426074421, new next: 1128426074421
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.connectionmanager.IdleRemover] run: IdleRemover notifying pools, interval: 150000
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Checking driver for URL: jdbc:mysql://localhost:3306/webdb
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Driver not yet registered for url: jdbc:mysql://localhost:3306/webdb
                2005-10-04 14:38:44,421 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Driver already registered for url: jdbc:mysql://localhost:3306/webdb
                2005-10-04 14:38:44,671 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                2005-10-04 14:38:44,703 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                2005-10-04 14:38:44,718 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                2005-10-04 14:38:44,750 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                2005-10-04 14:38:44,765 INFO [STDOUT] java.lang.IllegalStateException: No transaction.
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.tm.TxManager.rollback(TxManager.java:331)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.rollback(ServerVMClientUserTransaction.java:134)
                2005-10-04 14:38:44,765 INFO [STDOUT] at com.client.BmpEntityTestServlet.doGet(BmpEntityTestServlet.java:83)
                2005-10-04 14:38:44,765 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                2005-10-04 14:38:44,765 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                2005-10-04 14:38:44,765 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                2005-10-04 14:38:44,765 INFO [STDOUT] at java.lang.Thread.run(Thread.java:595)
                2005-10-04 14:38:44,781 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.parentTraceEnabled=true
                2005-10-04 14:38:44,781 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.nestedTraceEnabled=false
                2005-10-04 14:38:44,781 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.detectDuplicateNesting=true
                2005-10-04 14:38:44,781 INFO [STDOUT] org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=spider/14, BranchQual=, localId=14] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.ejb.EJBException: null; CausedByException is:
                null)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:344)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.TxManager.commit(TxManager.java:200)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(ServerVMClientUserTransaction.java:126)
                2005-10-04 14:38:44,781 INFO [STDOUT] at com.client.BmpEntityTestServlet.doGet(BmpEntityTestServlet.java:74)
                2005-10-04 14:38:44,781 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                2005-10-04 14:38:44,781 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                2005-10-04 14:38:44,781 INFO [STDOUT] at java.lang.Thread.run(Thread.java:595)
                2005-10-04 14:38:44,781 INFO [STDOUT] Caused by: javax.ejb.EJBException: null; CausedByException is:
                null
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.ejb.plugins.BMPPersistenceManager.invokeEjbStore(BMPPersistenceManager.java:504)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeEjbStore(CachedConnectionInterceptor.java:412)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.ejb.EntityContainer.invokeEjbStore(EntityContainer.java:729)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$2.invokeEjbStore(GlobalTxEntityMap.java:117)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.synchronize(GlobalTxEntityMap.java:239)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.beforeCompletion(GlobalTxEntityMap.java:302)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1384)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1076)
                2005-10-04 14:38:44,781 INFO [STDOUT] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:296)
                2005-10-04 14:38:44,781 INFO [STDOUT] ... 24 more


                • 5. Re: Simple BMP method throws TransactionRolledbackException
                  Darran Lofthouse Master

                  Could you also post the code of your entity bean, I think something is happening there - I will see if I can reproduce your problem.

                  • 6. Re: Simple BMP method throws TransactionRolledbackException
                    Bora Bora Newbie

                    Here goes the code, sorry for the mess. It uses a table in mysql database called userstable2 with username, forename, surname, email, password all varchar and username is the primary key.
                    Thanks...

                    package com.ejb;
                    
                    import java.io.Serializable;
                    import java.rmi.RemoteException;
                    import java.sql.Connection;
                    import java.sql.PreparedStatement;
                    import java.sql.ResultSet;
                    import java.sql.SQLException;
                    
                    import javax.ejb.CreateException;
                    import javax.ejb.DuplicateKeyException;
                    import javax.ejb.EJBException;
                    import javax.ejb.EntityBean;
                    import javax.ejb.EntityContext;
                    import javax.ejb.FinderException;
                    import javax.ejb.NoSuchEntityException;
                    import javax.ejb.ObjectNotFoundException;
                    import javax.ejb.RemoveException;
                    
                    public class UserBean implements Serializable, EntityBean {
                    
                     private static final String USERS_TABLE_NAME = "userstable2";
                    
                     private static final long serialVersionUID = 1L;
                    
                     private String userName;
                    
                     private String forename;
                    
                     private String surname;
                    
                     private String email;
                    
                     private String password;
                    
                     private Role role = new Role();
                    
                     private EntityContext context = null;
                    
                     public UserBean() {
                     }
                    
                     public UserBean(String userName, String forename, String surname,
                     String email, String password) {
                     this.userName = userName;
                     this.forename = forename;
                     this.surname = surname;
                     this.email = email;
                     this.password = password;
                     }
                    
                     /**
                     * @return Returns the email.
                     */
                     public String getEmail() {
                     return email;
                     }
                    
                     /**
                     * @param email
                     * The email to set.
                     */
                     public void setEmail(String email) {
                     this.email = email;
                     }
                    
                     /**
                     * @return Returns the forename.
                     */
                     public String getForename() {
                     return forename;
                     }
                    
                     /**
                     * @param forename
                     * The forename to set.
                     */
                     public void setForename(String forename) {
                     this.forename = forename;
                     }
                    
                     /**
                     * @return Returns the password.
                     */
                     public String getPassword() {
                     return password;
                     }
                    
                     /**
                     * @param password
                     * The password to set.
                     */
                     public void setPassword(String password) {
                     this.password = password;
                     }
                    
                     /**
                     * @return Returns the surname.
                     */
                     public String getSurname() {
                     return surname;
                     }
                    
                     /**
                     * @param surname
                     * The surname to set.
                     */
                     public void setSurname(String surname) {
                     this.surname = surname;
                     }
                    
                     /**
                     * @return Returns the userName.
                     */
                     public String getUserName() {
                     return userName;
                     }
                    
                     /**
                     * @param userName
                     * The userName to set.
                     */
                     public void setUserName(String userName) {
                     this.userName = userName;
                     }
                    
                     /**
                     * Add the role to this user.
                     *
                     * @param role
                     */
                     public void addRole(String roleName) {
                     role.addRole(roleName);
                     }
                    
                     /**
                     * User's roles include this role?
                     *
                     * @param role
                     * @return
                     */
                     public boolean isUserInRole(String roleName) {
                     return role.isInRole(roleName);
                     }
                    
                     /**
                     * Get the priority role name.
                     *
                     * @return
                     */
                     public String getRoleName() {
                     return role.getRoleName();
                     }
                    
                     public String ejbCreate(String userName, String forename, String surname,
                     String email, String password) throws CreateException {
                    
                     // get the connection for the db
                     Connection conn = UserDBUtil.getConnection();
                    
                     PreparedStatement stmtSelect = null;
                     PreparedStatement stmtInsert = null;
                     ResultSet rs = null;
                    
                     try {
                     // check for existing user
                     StringBuffer sbSelect = new StringBuffer();
                     sbSelect.append("SELECT username FROM ");
                     sbSelect.append(USERS_TABLE_NAME);
                     sbSelect.append(" WHERE username=?");
                     stmtSelect = conn.prepareStatement(sbSelect.toString());
                     stmtSelect.setString(1, userName);
                    
                     rs = stmtSelect.executeQuery();
                     if (rs.next()) {
                     throw new DuplicateKeyException("ejbCreate: Existing user.");
                     }
                    
                     StringBuffer sbInsert = new StringBuffer();
                     sbInsert.append("INSERT INTO ");
                     sbInsert.append(USERS_TABLE_NAME);
                     sbInsert
                     .append(" ( username, forename, surname, email, password ) ");
                     sbInsert.append(" VALUES (?, ?, ?, ?, ?) ");
                    
                     stmtInsert = conn.prepareStatement(sbInsert.toString());
                     stmtInsert.setString(1, userName);
                     stmtInsert.setString(2, forename);
                     stmtInsert.setString(3, surname);
                     stmtInsert.setString(4, email);
                     stmtInsert.setString(5, password);
                    
                     int rows = stmtInsert.executeUpdate();
                    
                     // there must be only 1 row affected
                     if (rows != 1) {
                     throw new CreateException("Failed to create user");
                     }
                    
                     // init variables
                     this.userName = userName;
                     this.forename = forename;
                     this.surname = surname;
                     this.email = email;
                     this.password = password;
                    
                     } catch (SQLException e) {
                     throw new EJBException("ejbCreate: " + e.getMessage());
                     } finally {
                     UserDBUtil.closeStatement(stmtSelect);
                     UserDBUtil.closeStatement(stmtInsert);
                     UserDBUtil.closeResultSet(rs);
                     UserDBUtil.closeConnection(conn);
                     }
                    
                     return userName;
                     }
                    
                     public void ejbPostCreate(String userName, String forename, String surname,
                     String email, String password) throws CreateException {
                     }
                    
                     public String ejbFindByPrimaryKey(String primaryKey) throws FinderException {
                     // get the connection for the db
                     Connection conn = UserDBUtil.getConnection();
                    
                     PreparedStatement stmtSelect = null;
                     ResultSet rs = null;
                    
                     try {
                     StringBuffer sbSelect = new StringBuffer();
                     sbSelect.append("SELECT username FROM ");
                     sbSelect.append(USERS_TABLE_NAME);
                     sbSelect.append(" WHERE username=?");
                     stmtSelect = conn.prepareStatement(sbSelect.toString());
                     stmtSelect.setString(1, primaryKey);
                    
                     System.out.println("Executing select query with key= " + primaryKey);
                    
                     rs = stmtSelect.executeQuery();
                     if (!rs.next()) {
                     throw new ObjectNotFoundException(
                     "ejbFindByPrimaryKey: User not found.");
                     } else{
                     System.out.println(rs.getString("username"));
                     }
                    
                     return primaryKey;
                    
                     } catch (SQLException e) {
                     throw new EJBException("ejbFindByPrimaryKey: " + e.getMessage());
                     } finally {
                     UserDBUtil.closeStatement(stmtSelect);
                     UserDBUtil.closeResultSet(rs);
                     UserDBUtil.closeConnection(conn);
                     }
                     }
                    
                     public void ejbActivate() throws EJBException, RemoteException {
                     }
                    
                     public void ejbPassivate() throws EJBException, RemoteException {
                     }
                    
                     public void ejbLoad() throws EJBException, RemoteException {
                     // get the connection for the db
                     Connection conn = UserDBUtil.getConnection();
                    
                     ResultSet rs = null;
                     PreparedStatement stmtSelect = null;
                    
                     // the primary key
                     String userName = (String) context.getPrimaryKey();
                    
                     try {
                    
                     StringBuffer sbSelect = new StringBuffer();
                    
                     sbSelect.append("SELECT ");
                     sbSelect.append("username, forename, surname, email, password FROM ");
                     sbSelect.append(USERS_TABLE_NAME);
                     sbSelect.append(" WHERE ");
                     sbSelect.append("username");
                     sbSelect.append("= ?");
                    
                     stmtSelect = conn.prepareStatement(sbSelect.toString());
                     stmtSelect.setString(1, userName);
                    
                     rs = stmtSelect.executeQuery();
                    
                     boolean beanFound = false;
                    
                     if (rs.next()) {
                     this.userName = rs.getString("username");
                     this.forename = rs.getString("forename");
                     this.surname = rs.getString("surname");
                     this.email = rs.getString("email");
                     this.password = rs.getString("password");
                     beanFound = true;
                     }
                    
                     // the bean with the given key value could not be found
                     if (!beanFound) {
                     throw new NoSuchEntityException("ejbLoad: " + "User not found.");
                     }
                    
                     } catch (SQLException e) {
                     throw new EJBException("ejbLoad: " + e.getMessage());
                     } finally {
                     UserDBUtil.closeStatement(stmtSelect);
                     UserDBUtil.closeResultSet(rs);
                     UserDBUtil.closeConnection(conn);
                     }
                     }
                    
                     public void ejbRemove() throws RemoveException, EJBException,
                     RemoteException {
                     // get the connection for the db
                     Connection conn = UserDBUtil.getConnection();
                    
                     PreparedStatement stmtRemove = null;
                    
                     try {
                     StringBuffer sbRemove = new StringBuffer();
                     sbRemove.append("DELETE FROM " + USERS_TABLE_NAME + " WHERE username=?");
                    
                     stmtRemove = conn.prepareStatement(sbRemove.toString());
                     stmtRemove.setString(1, userName);
                    
                     if (stmtRemove.executeUpdate() != 1) {
                     throw new EJBException("ejbRemove: Failed to remove user.");
                     }
                    
                     } catch (SQLException e) {
                     throw new EJBException("ejbRemove: " + e.getMessage());
                     }
                    
                     }
                    
                     public void ejbStore() throws EJBException, RemoteException {
                     // get the connection for the db
                     Connection conn = UserDBUtil.getConnection();
                    
                     PreparedStatement stmtUpdate = null;
                    
                     try {
                     StringBuffer sbUpdate = new StringBuffer();
                     sbUpdate.append("UPDATE " + USERS_TABLE_NAME);
                     sbUpdate
                     .append(" SET forename=?, surname=?, email=?, password=? WHERE username=?");
                     stmtUpdate.setString(1, forename);
                     stmtUpdate.setString(2, surname);
                     stmtUpdate.setString(3, email);
                     stmtUpdate.setString(4, password);
                     stmtUpdate.setString(5, userName);
                    
                     stmtUpdate = conn.prepareStatement(sbUpdate.toString());
                    
                     if (stmtUpdate.executeUpdate() != 1) {
                     throw new NoSuchEntityException("ejbStore: User not found.");
                     }
                    
                     } catch (SQLException e) {
                     throw new EJBException("ejbStore: " + e.getMessage());
                     } finally {
                     UserDBUtil.closeStatement(stmtUpdate);
                     UserDBUtil.closeConnection(conn);
                     }
                     }
                    
                     public void unsetEntityContext() throws EJBException, RemoteException {
                     context = null;
                     }
                    
                     public void setEntityContext(EntityContext arg0) throws EJBException,
                     RemoteException {
                     this.context = arg0;
                     }
                    
                    }
                    
                    


                    • 7. Re: Simple BMP method throws TransactionRolledbackException
                      Bora Bora Newbie

                      Hey, I discovered something..
                      There are no errors for findByPrimaryKey but when I use the remote interface that findByPrimaryKey returns to run a method, ejbLoad method is called and at the end of ejbLoad "No Transaction" exception is thrown.
                      Everything goes allright until the very end of ejbLoad method; the database lookup inside it works allright too; but when we get out of the ejbLoad method, the exception is thrown...

                      • 8. Re: Simple BMP method throws TransactionRolledbackException
                        Bora Bora Newbie

                        Now I am updating what I said I discovered in the previous post.
                        The ejbLoad method also returns ok, but when the transaction is committed "No Transaction" exception is thrown. I guess the container cannot find a Transaction associated with the executing thread.
                        I dont know why...I mark all the methods of the BMP bean Transaction-Required and I am using UserTransaction enclosing the method calls on the bean. What am I doing wrong here?

                        Here is the code:


                        User beanRemote = null;

                        transaction = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
                        transaction.begin();

                        beanRemote = userHome.findByPrimaryKey("erbora00");

                        String str = beanRemote.getPassword();

                        System.out.println("Got the password");


                        // every thing is ok until this line
                        transaction.commit();


                        • 9. Re: Simple BMP method throws TransactionRolledbackException
                          Bora Bora Newbie

                          Here is the most up-to-date stack trace:
                          2005-10-06 10:49:33,125 INFO [STDOUT] Got the password
                          2005-10-06 10:49:33,125 INFO [STDOUT] java.lang.IllegalStateException: No transaction.
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.jboss.tm.TxManager.rollback(TxManager.java:331)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.rollback(ServerVMClientUserTransaction.java:134)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at com.client.BmpEntityTestServlet.doGet(BmpEntityTestServlet.java:82)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                          2005-10-06 10:49:33,125 DEBUG [org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory] Using properties: {user=root, password=--hidden--}
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
                          2005-10-06 10:49:33,125 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at java.lang.Thread.run(Thread.java:595)
                          2005-10-06 10:49:33,140 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.parentTraceEnabled=true
                          2005-10-06 10:49:33,140 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.nestedTraceEnabled=false
                          2005-10-06 10:49:33,140 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.detectDuplicateNesting=true
                          2005-10-06 10:49:33,140 INFO [STDOUT] org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=spider/14, BranchQual=, localId=14] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.ejb.EJBException: null; CausedByException is:
                          null)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:344)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.jboss.tm.TxManager.commit(TxManager.java:200)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(ServerVMClientUserTransaction.java:126)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at com.client.BmpEntityTestServlet.doGet(BmpEntityTestServlet.java:78)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                          2005-10-06 10:49:33,140 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at java.lang.Thread.run(Thread.java:595)
                          2005-10-06 10:49:33,156 INFO [STDOUT] Caused by: javax.ejb.EJBException: null; CausedByException is:
                          null
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.ejb.plugins.BMPPersistenceManager.invokeEjbStore(BMPPersistenceManager.java:504)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeEjbStore(CachedConnectionInterceptor.java:412)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.ejb.EntityContainer.invokeEjbStore(EntityContainer.java:729)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$2.invokeEjbStore(GlobalTxEntityMap.java:117)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.synchronize(GlobalTxEntityMap.java:239)
                          2005-10-06 10:49:33,156 INFO [STDOUT] at org.jboss.ejb.GlobalTxEntityMap$GlobalTxSynchronization.beforeCompletion(GlobalTxEntityMap.java:302)
                          2005-10-06 10:49:33,171 INFO [STDOUT] at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1384)
                          2005-10-06 10:49:33,171 INFO [STDOUT] at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1076)
                          2005-10-06 10:49:33,171 INFO [STDOUT] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:296)
                          2005-10-06 10:49:33,171 INFO [STDOUT] ... 24 more

                          • 10. Re: Simple BMP method throws TransactionRolledbackException
                            Bora Bora Newbie

                            By the way the above stack trace occurs when the transaction.commit() line throws an "Unable to commit" exception and then transaction.rollBack() is executed.

                            • 11. Re: TransactionRolledBackException...PROBLEM SOLVED
                              Bora Bora Newbie

                              Problem solved. Here are the conclusions for entity bean transactions:

                              1. If JBoss says it is a TransactionRolledBackException do not stop looking around to find a bug that seems to have nothing to do with Transaction stuff.
                              In my case ejbStore method had a bug which resulted in a NullPointerException and this exception as a system exception thrown by an EJB method has been shown to me as a TransactionRolledBackException; because JBoss tries to roll back automatically when it encounters a system exception (this is what the EJB spec specifies to be done; so dont blame JBoss).

                              2. You do not need to use UserTransaction object to begin and commit a transaction when using an EJB by a client. Simply edit the ejb-jar.xml and add container-transaction stuff in the assembly-descriptor element. The container will handle the transactions for the ejb methods.

                              3. If you don't want to use container managed transactions then mark the container-transaction in the assembly-descriptor as NotSupported and then use UserTransaction object to begin and commit the transactions.

                              4. Method to discover what is bugging you with TransactionRolledBackException: mark your ejb methods as NotSupported for transactions. Then use your own UserTransaction object at the client by retrieving it from the container using JNDI call java:comp/UserTransaction. Then the exception will point you at the bug!!! Because the exception will not be changed to TransactionRolledBackException; this is what I have done to locate the bug.

                              • 12. Re: TransactionRolledBackException...PROBLEM SOLVED
                                Darran Lofthouse Master

                                 

                                "erbora00" wrote:

                                2. You do not need to use UserTransaction object to begin and commit a transaction when using an EJB by a client. Simply edit the ejb-jar.xml and add container-transaction stuff in the assembly-descriptor element. The container will handle the transactions for the ejb methods.


                                Be careful if you are accessing entity beans directly, every single invocation will start a new transaction and commit the transaction.



                                • 14. Re: Simple BMP method throws TransactionRolledbackException
                                  jaap Newbie

                                  Hi ebora00,

                                  Thanks for your hard work. Your solution ispired me to solve my performance problem (http://www.jboss.com/index.html?module=bb&op=viewtopic&t=69953)

                                  I have disabled tansaction by setting transaction-type to 'Bean' in ejb-jar. Consider not using transactions, transactions came with a los of almost 50% in performance.

                                  Jaap