AnsweredAssumed Answered

When to open transaction and when not ...

Question asked by chris.joelly on Apr 1, 2012
Hello,

i have problems with transactions, and i dont know if this is the right forum for it, but as i only face this problem together with Activiti i start with it here. I use activiti-cdi with JSF2 on Glassfish using the following cfg:

   <jee:jndi-lookup jndi-name="jdbc/dataSource" id="dataSource" />
   <jee:jndi-lookup jndi-name="java:appserver/TransactionManager" id="transactionManager" />
   <bean id="processEngineConfiguration"
      class="org.activiti.cdi.CdiJtaProcessEngineConfiguration">
      <property name="dataSource" ref="dataSource" />
      <property name="transactionManager" ref="transactionManager"/>
      <property name="transactionsExternallyManaged" value="true" />
      <property name="databaseSchemaUpdate" value="true" />
      <property name="databaseType" value="mysql" />
      <property name="jobExecutorActivate" value="false" />
   </bean>
I can use things like #{repositoryService.deleteDeployment(process.deploymentId, true)}, #{formService.getStartFormData(process.id).formKey}, #{businessProcess.startProcessByKey(process.key)} and {businessProcess.completeTask(true)} without a problem. Everything is persisted to the MySQL database without a problem.

But when i start a process instance with #{businessProcess.startProcessByKey(process.key)} and use a faces navigation rule to dispatch to the task list i get the following exception:

[#|2012-04-01T18:25:21.092+0200|SEVERE|glassfish3.1.1|javax.enterprise.resource.resourceadapter.com.sun.enterprise.resource.rm|_ThreadID=19;_ThreadName=Thread-2;|RAR5029:Unexpected exception while registering component
java.lang.RuntimeException: Got exception during XAResource.start:
   at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.enlistResource(JavaEETransactionManagerSimplified.java:377)
   at com.sun.enterprise.resource.rm.ResourceManagerImpl.registerResource(ResourceManagerImpl.java:152)
   at com.sun.enterprise.resource.rm.ResourceManagerImpl.enlistResource(ResourceManagerImpl.java:112)
   at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:208)
   at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:338)
   at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:301)
   at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:190)
   at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:165)
   at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:160)
   at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:113)
   at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:73)
   at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)
   at org.activiti.engine.impl.db.DbSqlSession.<init>(DbSqlSession.java:86)
   at org.activiti.engine.impl.db.DbSqlSessionFactory.openSession(DbSqlSessionFactory.java:80)
   at org.activiti.engine.impl.interceptor.CommandContext.getSession(CommandContext.java:179)
   at org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext.getDbSqlSession(StandaloneMybatisTransactionContext.java:77)
   at org.activiti.engine.impl.cfg.standalone.StandaloneMybatisTransactionContext.rollback(StandaloneMybatisTransactionContext.java:91)
   at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:122)
   at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:49)
   at org.activiti.engine.impl.interceptor.JtaTransactionInterceptor.execute(JtaTransactionInterceptor.java:59)
   at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
   at org.activiti.engine.impl.AbstractQuery.list(AbstractQuery.java:112)
   at erp.web.faces.beans.activiti.TaskList.getOwnTasks(TaskList.java:83)
   at erp.web.faces.beans.activiti.org$jboss$weld$bean-erp$web-ManagedBean-class_erp$web$faces$beans$activiti$TaskList_$$_WeldClientProxy.getOwnTasks(org$jboss$weld$bean-erp$web-ManagedBean-class_erp$web$faces$beans$activiti$TaskList_$$_WeldClientProxy.java)
   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 javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
   at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
   at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
   at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
   at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
   at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
   at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
   at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
   at javax.faces.component.UIData.getValue(UIData.java:731)
   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.faces.facelets.util.DevTools.writeAttributes(DevTools.java:375)
   at com.sun.faces.facelets.util.DevTools.writeStart(DevTools.java:424)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:245)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.writeComponent(DevTools.java:264)
   at com.sun.faces.facelets.util.DevTools.debugHtml(DevTools.java:128)
   at com.sun.faces.renderkit.RenderKitUtils.renderHtmlErrorPage(RenderKitUtils.java:1159)
   at com.sun.faces.context.ExceptionHandlerImpl.throwIt(ExceptionHandlerImpl.java:269)
   at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:141)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
   at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
   at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
   at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
   at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
   at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
   at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
   at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
   at java.lang.Thread.run(Thread.java:662)
Caused by: javax.transaction.xa.XAException: com.sun.appserv.connectors.internal.api.PoolingException: java.lang.NullPointerException
   at com.sun.enterprise.resource.ConnectorXAResource.handleResourceException(ConnectorXAResource.java:115)
   at com.sun.enterprise.resource.ConnectorXAResource.start(ConnectorXAResource.java:150)
   at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.enlistResource(JavaEETransactionManagerSimplified.java:375)
   … 89 more
The interesting thing is that i get the exception only once. When i go back to the previous browser view and submit the JSF form again which starts the process instance using #{businessProcess.startProcessByKey(process.key)}, another instance is created appart from the instance which is created just before the exception is thrown. And then i can continue work without that exception.

I tried using an UserTransaction which removed the exception in the method which produces the task list:

   @Inject private TaskService taskService;
   @Resource UserTransaction utx;
   @Inject UserSession userSession;

   @Produces
   @Named("personalTaskList")
   public List<Task> getOwnTasks() throws SecurityException,
         IllegalStateException, RollbackException, HeuristicMixedException,
         HeuristicRollbackException, SystemException, NotSupportedException {
      List<Task> tasks = null;
      utx.begin();
      if (userSession.isAdmin()) {
         tasks = taskService.createTaskQuery().orderByTaskCreateTime().asc()
               .list();
      } else {
         tasks = taskService.createTaskQuery()
               .taskAssignee(userSession.getUser().getUsername())
               .orderByTaskCreateTime().asc().list();
      }
      utx.commit();
      return tasks;
   }
Has anybody an idea why i need a transaction here? And why only to avoid the exception one time? If i omit the user transaction then i can work with the app without exception when it occurred once.

Thanks, Chris

Outcomes