0

I am getting the following exception:

javax.el.ELException: javax.persistence.TransactionRequiredException
at com.sun.el.parser.AstValue.invoke(AstValue.java:279)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)

Caused by: javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:299)

I am using Hibernate 4.3.5

@Named
@SessionScoped
public class MenuBean implements Serializable {

   @PersistenceContext
   private EntityManager entityManager;

   @Transactional
   public void create() {
       MenuTitle menu = new MenuTitle();
       menu.setLabel(label);
       entityManager.persist(menu); //exception in this line
       label = null;
   }

Persistence XML:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>MySQL5</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.current_session_context_class" value="jta"/>
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
        <property name="hibernate.hbm2ddl.auto" value="none"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
    </properties>
</persistence-unit>

I have also tried to set hibernate.transaction.jta.platform to org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform but it results in the same error.

EJB CMT´s are working fine.

The Create action is called from a commandButton:

<p:commandButton value="Create" process="@this type" update=":megaMenuForm:mainMenu" actionListener="#{menuBean.create()}" oncomplete="closeMenuDialog(xhr, status, args)"/>  
tak3shi
  • 2,305
  • 1
  • 20
  • 33
  • how are you testing this? Where are you injecting your Menu bean? – ra2085 Jun 13 '14 at 19:36
  • Also, Have you tried using @Model instead of SessionScoped? I don't see any session state that you've to keep alive in your MenuBean. – ra2085 Jun 13 '14 at 19:46
  • i am calling the menuBean from commandButton actionListener, see updated question. – tak3shi Jun 13 '14 at 19:49
  • Go ahead and try the @Model annotation instead on SessionScoped and post the results. (unless you really have something to keep alive in MenuBean) – ra2085 Jun 13 '14 at 19:53
  • also, are you certain you're not finishing the transaction inside your create method on in an interceptor? It would be useful for you to share that code. – ra2085 Jun 13 '14 at 20:01
  • I was able to replace SessionScoped with Model but i am still getting TransactionRequiredException. – tak3shi Jun 13 '14 at 20:05
  • could you share the contents of the create() method? – ra2085 Jun 13 '14 at 20:07
  • i have updated the code of the create method – tak3shi Jun 13 '14 at 20:15
  • Have you tried to force a new transaction? with @Transactional(Transactional.TxType.REQUIRES_NEW) – ra2085 Jun 13 '14 at 20:19
  • Also, please post more or the full stack trace. Doesn't make sense that you're trying a persist operation, and the stack shows a merge operation. I'm worried you're getting into a stale state outside of a transaction. – ra2085 Jun 13 '14 at 20:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55601/discussion-between-tak3shi-and-ra2085). – tak3shi Jun 13 '14 at 20:26
  • Did you resolve this problem? – unwichtich Jun 15 '14 at 00:01
  • The problem is still unresolved. – tak3shi Jun 15 '14 at 19:23

1 Answers1

-1

See EJB specs if you want to manage transaction. Inject a resource UserTransaction. Also @Transactional with not do anything here since you are using EJB.
In general you example should work, and @Transactional annotation will be just ignored. Could you try to make an interface, and call create method through it? Looks like you are using EJB 2 style coding, but Glassfish 4 implements EJB 3.1, so it is better to go with a new one.

win_wave
  • 1,498
  • 11
  • 9
  • I dont see any EJB in the above example. I am using the new JEE7 style with CDI and @Transactional annotation. – tak3shi Jun 18 '14 at 07:04
  • BTW, can you try to use @Stateless instead of SessionScoped – win_wave Jun 18 '14 at 07:57
  • I have tried this already. It has caused the same exception. I think there is some configuration issue with hibernate. Glassfish uses EclipseLink by default, but i want use hibernate-search for fulltext searches. – tak3shi Jun 18 '14 at 08:08