title: javax.ejb.TransactionRolledbackLocalException (Glassfish 3 + JPA + EclipseLink)
body: <p>I've got a problem on an application developed using Glassfish 3.1.2 + JPA + EclipseLink + MySQL, Immediately after i deploy the application (No tables on DB) it runs just fine and apparently executes the only query (Count entities in table) that i entered.
  But if i reload the page, calling the servlet again causes an exception to be thrown:
  TransactionRolledbackLocalException</p>
  
  <p>Apparently on the same method used to count entities.</p>
  
  <p>Then if i reload again, it runs just fine, then again exception, fine, exception and so on...</p>
  
  <p>This is my servlet:</p>
  
  <pre><code>@WebServlet(name="Controller", urlPatterns={"/Controller"})
  public class Controller extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      @EJB WineDao _wines;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          _wines.countWines();
  
          RequestDispatcher view = request.getRequestDispatcher("ShowAll.jsp");
          view.forward(request, response);
      }
  } 
  </code></pre>
  
  <p>This is WineDao:</p>
  
  <pre><code>@Stateless
  public class WineDao {
      @PersistenceContext private EntityManager em;
  
      public Long countWines() {
          CriteriaBuilder qb = em.getCriteriaBuilder();
          CriteriaQuery&lt;Long&gt; cq = qb.createQuery(Long.class);
          cq.select(qb.count(cq.from(Wine.class)));
          return em.createQuery(cq).getSingleResult();
      }
  
  }
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;jdbc/MySQLDataSource&lt;/jta-data-source&gt;
          &lt;properties&gt;
              &lt;!-- drop and create tables at deployment --&gt;
              &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables"/&gt;
              &lt;!-- log sqls executed in server.log --&gt;
              &lt;property name="eclipselink.logging.level" value="FINE"/&gt;
              &lt;!-- Instruct EclipseLink to not log execeptions it throws. Lets the application decide about it--&gt;
              &lt;property name="eclipselink.logging.exceptions" value="true"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And finally the server log, when i get no errors:</p>
  
  <pre><code>[#|2013-01-07T18:32:27.271+0100|FINE|glassfish3.1.2|org.eclipse.persistence.session.file:/opt/glassfish/glassfish/domains/domain1/applications/appname/WEB-INF/classes/_myPU.sql|_ThreadID=21;_ThreadName=Thread-2;ClassName=null;MethodName=null;|SELECT COUNT(ID) FROM Wine|#]
  </code></pre>
  
  <p>And when i get the error:</p>
  
  <pre><code>[#|2013-01-07T18:32:29.582+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=20;_ThreadName=Thread-2;|EJB5184:A system exception occurred during an invocation on EJB WineDao, method: public java.lang.Long it.vitux.java.appname.dao.WineDao.countWines()|#]
  
  [#|2013-01-07T18:32:29.583+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=20;_ThreadName=Thread-2;|javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted
      at com.sun.ejb.containers.BaseContainer.useClientTx(BaseContainer.java:4722)
      at com.sun.ejb.containers.BaseContainer.preInvokeTx(BaseContainer.java:4601)
      at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1914)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
      at $Proxy516.countWines(Unknown Source)
      at it.vitux.java.appname.dao.__EJB31_Generated__WineDao__Intf____Bean__.countWines(Unknown Source)
      at it.vitux.java.appname.controllers.Controller.doGet(Controller.java:43)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
      at com.sun.grizzly.http.ajp.AjpProcessorTask.invokeAdapter(AjpProcessorTask.java:125)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
      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:679)
  |#]
  
  [#|2013-01-07T18:32:29.591+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=20;_ThreadName=Thread-2;|StandardWrapperValve[Controller]: PWC1406: Servlet.service() for servlet Controller threw exception
  javax.ejb.EJBTransactionRolledbackException
      at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContainer.java:2314)
      at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2096)
      at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
      at $Proxy516.countWines(Unknown Source)
      at it.vitux.java.appname.dao.__EJB31_Generated__WineDao__Intf____Bean__.countWines(Unknown Source)
      at it.vitux.java.appname.controllers.Controller.doGet(Controller.java:43)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
      at com.sun.grizzly.http.ajp.AjpProcessorTask.invokeAdapter(AjpProcessorTask.java:125)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
      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:679)
  Caused by: javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted
      at com.sun.ejb.containers.BaseContainer.useClientTx(BaseContainer.java:4722)
      at com.sun.ejb.containers.BaseContainer.preInvokeTx(BaseContainer.java:4601)
      at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1914)
      at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
      ... 31 more
  |#]
  </code></pre>
  
  <p>Forgot the Wine class:</p>
  
  <pre><code>@Entity
  public class Wine {
      public static final int UP_ADDED = 0;
      public static final int UP_REMOVED = 1;
      public static final int UP_PRICE = 2;
      public static final int UP_OFFER = 3;
  
      @Id Long id; 
      private String name;
      private BigDecimal price;
      private BigDecimal offer;
      @Temporal(TemporalType.TIMESTAMP)
      private Date updateTime;
      private int updateStatus = 0;
  
      // Getters + Setters
  }
  </code></pre>