You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Lucian BRANCOVEAN <Lu...@indsoft.ro> on 2020/05/04 20:24:04 UTC

TransactionRequiredException when a transaction should exist

Hi,

I am using Deltaspike 1.9.3 in Wildfly 14.

I have a CDI bean with a method that is supposed to insert or update an entity, like this:

@Named
@ApplicationScoped
public class StuffService {

    @Inject
    private StuffRepository repository; // StuffRepository extends EntityRepository<Stuff, Long>

...

    @Transactional
    public void save(Stuff stuff) throws Exception {

        if (stuff.getCode() == null) {
            repository.save(stuff);

        } else {

            Stuff entity = getByCode(stuff.getCode()); // code is @Id
            entity.setName(stuff.getName());
            repository.save(entity);
        }
    }

This works, but looking at the EntityRepository.save javadoc, I understand that it already does what I want, decide if to update or insert a new record based on the presence/absence of a primary key value.

However, if I call repository.save() on an existing entity (so I remove the if and always go in the first branch), I get an exception:

javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
    at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
    at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)

What happens here? How come a transaction does not exist unless I create a new object and transfer the data into it?

What I guess might be relevant is that the Stuff entity comes from a ViewScoped bean, from a previous request.

Note, I am using container-managed transactions, and javax.transaction.Transactional. Should I use org.apache.deltaspike.jpa.api.transaction.Transactional? What is the difference?

Thanks in advance,

Lucian




Re: TransactionRequiredException when a transaction should exist

Posted by Luís Alves <lu...@gmail.com>.
Lucian,

I think you are trying to persist a detached entity.

Try this:

@Transactional
public void update(StuffDto stuffDto) throws Exception {
     Stuff entity = repository.findById(stuffDto.getId());
     entity.setName(stuffDto.getName()); //To do mapping use something like
mapstruct
     repository.save(entity);
}

Did it work?


On Thu, May 7, 2020 at 8:11 PM Lucian BRANCOVEAN <
Lucian.BRANCOVEAN@indsoft.ro> wrote:

> > I think you need to take care about it [1] .
>
>
> I don't undestand what you are proposing, and why it would help. I should
> use an extended persistence context? Why? Why is there a transaction
> available if I do an insert or update with a new object, but no transaction
> available if I do update with an existing object, and what does that have
> to do with extended persistence contexts?
>
> Lucian
>
> ________________________________________
> From: Gilberto <gi...@gmail.com>
> Sent: Thursday, 7 May, 2020 5:12 PM
> To: users@deltaspike.apache.org
> Subject: Re: TransactionRequiredException when a transaction should exist
>
> > What I guess might be relevant is that the Stuff entity comes from a
> ViewScoped bean, from a previous request.
>
> > javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction
> is required to perform this operation (either use a transaction or >
> extended persistence context)
>
> I think you need to take care about it [1] .
> I my case (TomEE) I use EJB with Eclipselink and it takes care for me [2] -
> I do not use @Transactional
> Like:
> @Stateless
> public class EventoService {
>
>     protected transient final Logger logger =
> LoggerFactory.getLogger(this.getClass());
>
>     @Inject
>     private EventoRepository eventoRepo;
>
>     @Inject
>     private AuthorizationBean autorizationBen;
>
>     public void setAutorizationBen(AuthorizationBean autorizationBen) {
>         this.autorizationBen = autorizationBen;
>     }
>
>     public Evento save(Evento evento) {
>         evento.setCreatedBy(autorizationBen.getUsuarioID());
>         evento.setUpdatedBy(evento.getCreatedBy());
>         evento.setDtCreatedBy(LocalDateTime.now());
>         evento.setDtUpdatedBy(LocalDateTime.now());
>         return eventoRepo.save(evento);
>     }
>
>     public Evento saveAndFlushAndRefresh(Evento evento) {
>         evento.setUpdatedBy(autorizationBen.getUsuarioID());
>         evento.setDtUpdatedBy(LocalDateTime.now());
>         return eventoRepo.saveAndFlushAndRefresh(evento);
>     }
>
>     public void delete(Evento evento) {
>         eventoRepo.attachAndRemove(evento);
>     }
>
>     public Evento findById(Long id) {
>         return eventoRepo.findBy(id);
>     }
>
>     public List<Evento> findAll() {      return eventoRepo.findAll();}
> ...
>
> Regards,
>
> Gilberto
>
>
> [1]
>
> https://deltaspike.apache.org/documentation/jpa.html#ExtendedPersistenceContexts
> [2]
> https://tomee.apache.org/examples-trunk/transaction-rollback/README.html
>
> Em qui., 7 de mai. de 2020 às 10:10, Lucian BRANCOVEAN <
> Lucian.BRANCOVEAN@indsoft.ro> escreveu:
>
> > Ok, I've changed that to:
> >
> > @ApplicationScoped
> > public class EntityManagerProducer {
> >
> >         @Produces
> >         @Dependent
> >         @PersistenceContext(unitName = "pu")
> >         private EntityManager entityManager;
> >
> > It seems to make no difference. Other operations still work, the update
> > still fails:
> >
> >  Operation failed: javax.persistence.TransactionRequiredException:
> > WFLYJPA0060: Transaction is required to perform this operation (either
> use
> > a transaction or extended persistence context)
> >         at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> >         at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.EntityRepositoryHandler.save(EntityRepositoryHandler.java:77)
> >         at
> > java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> > Method)
> >         at
> >
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> >         at
> >
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> >         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.invoke(DelegateQueryBuilder.java:130)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.execute(DelegateQueryBuilder.java:60)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.QueryBuilder.executeQuery(QueryBuilder.java:57)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> > Source)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$1.proceed(TransactionalQueryRunner.java:73)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy.execute(ContainerManagedTransactionStrategy.java:40)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeTransactional(TransactionalQueryRunner.java:68)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeQuery(TransactionalQueryRunner.java:50)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> > Source)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.process(QueryHandler.java:151)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:130)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> > Source)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.proceed(DeltaSpikeProxyInvocationHandler.java:97)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.invoke(DeltaSpikeProxyInvocationHandler.java:78)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> > Source)
> >         at
> >
> [..]-0.0.1-SNAPSHOT.war//[..]StuffRepository$$DSPartialBeanProxy.save(Unknown
> > Source)
> >
> >
> > Lucian
> >
> > ________________________________________
> > From: Gilberto <gi...@gmail.com>
> > Sent: Thursday, 7 May, 2020 3:45 PM
> > To: users@deltaspike.apache.org
> > Subject: Re: TransactionRequiredException when a transaction should exist
> >
> > @ApplicationScoped is for your class which has several producers
> > (requested-scope I think).
> >
> > You need the simplest one:
> >
> > public class EntityManagerProducer {
> >
> >     @Produces
> >     @Dependent
> >     @PersistenceContext(unitName = "gacePU")
> >     public EntityManager entityManager;
> > }
> >
> > Em qui., 7 de mai. de 2020 às 08:58, Lucian BRANCOVEAN <
> > Lucian.BRANCOVEAN@indsoft.ro> escreveu:
> >
> > > Hi,
> > >
> > > This is my EM producer:
> > >
> > >         @PersistenceContext(unitName = "pu")
> > >         private EntityManager entityManager;
> > >
> > >         @ApplicationScoped
> > >         @Produces
> > >         public EntityManager getEntityManager() {
> > >                 return entityManager;
> > >         }
> > >
> > >
> > > I don't have a @Disposes method because I am using container-managed
> > > persistence and it results in errors complainig that I should not close
> > the
> > > container-managed EM manually. Am I wrong in having it
> > application-scoped?
> > >
> > > I have a src/main/resources/META-INF/apache-deltaspike.properties file
> > > with this:
> > >
> > >
> > >
> >
> globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> > >
> > > And I see this in the log:
> > >  INFO
> > > [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension]
> (MSC
> > > service thread 1-2)
> > >
> >
> org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> > > is configured as global-alternative
> > >
> > > This is what I understood to be required in my case. Should I use
> > > BeanManagedUserTransactionStrategy instead? What is the difference?
> > >
> > > Anything else I should check?
> > >
> > > Lucian
> > >
> > > ________________________________________
> > > From: Gerhard Petracek <gp...@apache.org>
> > > Sent: Thursday, 7 May, 2020 11:58 AM
> > > To: users@deltaspike.apache.org
> > > Subject: Re: TransactionRequiredException when a transaction should
> exist
> > >
> > > hi lucian,
> > >
> > > please ensure that you are using the correct setup for ee-servers
> > > (esp. see [1] and [2]).
> > >
> > > regards,
> > > gerhard
> > >
> > > [1]
> > >
> >
> http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
> > > [2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport
> > >
> > >
> > >
> > > Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
> > > <Lu...@indsoft.ro>:
> > > >
> > > > Hi,
> > > >
> > > > I am using Deltaspike 1.9.3 in Wildfly 14.
> > > >
> > > > I have a CDI bean with a method that is supposed to insert or update
> an
> > > entity, like this:
> > > >
> > > > @Named
> > > > @ApplicationScoped
> > > > public class StuffService {
> > > >
> > > >     @Inject
> > > >     private StuffRepository repository; // StuffRepository extends
> > > EntityRepository<Stuff, Long>
> > > >
> > > > ...
> > > >
> > > >     @Transactional
> > > >     public void save(Stuff stuff) throws Exception {
> > > >
> > > >         if (stuff.getCode() == null) {
> > > >             repository.save(stuff);
> > > >
> > > >         } else {
> > > >
> > > >             Stuff entity = getByCode(stuff.getCode()); // code is @Id
> > > >             entity.setName(stuff.getName());
> > > >             repository.save(entity);
> > > >         }
> > > >     }
> > > >
> > > > This works, but looking at the EntityRepository.save javadoc, I
> > > understand that it already does what I want, decide if to update or
> > insert
> > > a new record based on the presence/absence of a primary key value.
> > > >
> > > > However, if I call repository.save() on an existing entity (so I
> remove
> > > the if and always go in the first branch), I get an exception:
> > > >
> > > > javax.persistence.TransactionRequiredException: WFLYJPA0060:
> > Transaction
> > > is required to perform this operation (either use a transaction or
> > extended
> > > persistence context)
> > > >     at org.jboss.as.jpa@14.0.1.Final
> > >
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> > > >     at org.jboss.as.jpa@14.0.1.Final
> > >
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> > > >
> > > > What happens here? How come a transaction does not exist unless I
> > create
> > > a new object and transfer the data into it?
> > > >
> > > > What I guess might be relevant is that the Stuff entity comes from a
> > > ViewScoped bean, from a previous request.
> > > >
> > > > Note, I am using container-managed transactions, and
> > > javax.transaction.Transactional. Should I use
> > > org.apache.deltaspike.jpa.api.transaction.Transactional? What is the
> > > difference?
> > > >
> > > > Thanks in advance,
> > > >
> > > > Lucian
> > > >
> > > >
> > > >
> > >
> >
>

Re: TransactionRequiredException when a transaction should exist

Posted by Lucian BRANCOVEAN <Lu...@indsoft.ro>.
> I think you need to take care about it [1] .


I don't undestand what you are proposing, and why it would help. I should use an extended persistence context? Why? Why is there a transaction available if I do an insert or update with a new object, but no transaction available if I do update with an existing object, and what does that have to do with extended persistence contexts?

Lucian

________________________________________
From: Gilberto <gi...@gmail.com>
Sent: Thursday, 7 May, 2020 5:12 PM
To: users@deltaspike.apache.org
Subject: Re: TransactionRequiredException when a transaction should exist

> What I guess might be relevant is that the Stuff entity comes from a
ViewScoped bean, from a previous request.

> javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction
is required to perform this operation (either use a transaction or >
extended persistence context)

I think you need to take care about it [1] .
I my case (TomEE) I use EJB with Eclipselink and it takes care for me [2] -
I do not use @Transactional
Like:
@Stateless
public class EventoService {

    protected transient final Logger logger =
LoggerFactory.getLogger(this.getClass());

    @Inject
    private EventoRepository eventoRepo;

    @Inject
    private AuthorizationBean autorizationBen;

    public void setAutorizationBen(AuthorizationBean autorizationBen) {
        this.autorizationBen = autorizationBen;
    }

    public Evento save(Evento evento) {
        evento.setCreatedBy(autorizationBen.getUsuarioID());
        evento.setUpdatedBy(evento.getCreatedBy());
        evento.setDtCreatedBy(LocalDateTime.now());
        evento.setDtUpdatedBy(LocalDateTime.now());
        return eventoRepo.save(evento);
    }

    public Evento saveAndFlushAndRefresh(Evento evento) {
        evento.setUpdatedBy(autorizationBen.getUsuarioID());
        evento.setDtUpdatedBy(LocalDateTime.now());
        return eventoRepo.saveAndFlushAndRefresh(evento);
    }

    public void delete(Evento evento) {
        eventoRepo.attachAndRemove(evento);
    }

    public Evento findById(Long id) {
        return eventoRepo.findBy(id);
    }

    public List<Evento> findAll() {      return eventoRepo.findAll();}
...

Regards,

Gilberto


[1]
https://deltaspike.apache.org/documentation/jpa.html#ExtendedPersistenceContexts
[2] https://tomee.apache.org/examples-trunk/transaction-rollback/README.html

Em qui., 7 de mai. de 2020 às 10:10, Lucian BRANCOVEAN <
Lucian.BRANCOVEAN@indsoft.ro> escreveu:

> Ok, I've changed that to:
>
> @ApplicationScoped
> public class EntityManagerProducer {
>
>         @Produces
>         @Dependent
>         @PersistenceContext(unitName = "pu")
>         private EntityManager entityManager;
>
> It seems to make no difference. Other operations still work, the update
> still fails:
>
>  Operation failed: javax.persistence.TransactionRequiredException:
> WFLYJPA0060: Transaction is required to perform this operation (either use
> a transaction or extended persistence context)
>         at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
>         at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.EntityRepositoryHandler.save(EntityRepositoryHandler.java:77)
>         at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>         at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>         at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.invoke(DelegateQueryBuilder.java:130)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.execute(DelegateQueryBuilder.java:60)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.QueryBuilder.executeQuery(QueryBuilder.java:57)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$1.proceed(TransactionalQueryRunner.java:73)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy.execute(ContainerManagedTransactionStrategy.java:40)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeTransactional(TransactionalQueryRunner.java:68)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeQuery(TransactionalQueryRunner.java:50)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.process(QueryHandler.java:151)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:130)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.proceed(DeltaSpikeProxyInvocationHandler.java:97)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.invoke(DeltaSpikeProxyInvocationHandler.java:78)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//[..]StuffRepository$$DSPartialBeanProxy.save(Unknown
> Source)
>
>
> Lucian
>
> ________________________________________
> From: Gilberto <gi...@gmail.com>
> Sent: Thursday, 7 May, 2020 3:45 PM
> To: users@deltaspike.apache.org
> Subject: Re: TransactionRequiredException when a transaction should exist
>
> @ApplicationScoped is for your class which has several producers
> (requested-scope I think).
>
> You need the simplest one:
>
> public class EntityManagerProducer {
>
>     @Produces
>     @Dependent
>     @PersistenceContext(unitName = "gacePU")
>     public EntityManager entityManager;
> }
>
> Em qui., 7 de mai. de 2020 às 08:58, Lucian BRANCOVEAN <
> Lucian.BRANCOVEAN@indsoft.ro> escreveu:
>
> > Hi,
> >
> > This is my EM producer:
> >
> >         @PersistenceContext(unitName = "pu")
> >         private EntityManager entityManager;
> >
> >         @ApplicationScoped
> >         @Produces
> >         public EntityManager getEntityManager() {
> >                 return entityManager;
> >         }
> >
> >
> > I don't have a @Disposes method because I am using container-managed
> > persistence and it results in errors complainig that I should not close
> the
> > container-managed EM manually. Am I wrong in having it
> application-scoped?
> >
> > I have a src/main/resources/META-INF/apache-deltaspike.properties file
> > with this:
> >
> >
> >
> globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> >
> > And I see this in the log:
> >  INFO
> > [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension] (MSC
> > service thread 1-2)
> >
> org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> > is configured as global-alternative
> >
> > This is what I understood to be required in my case. Should I use
> > BeanManagedUserTransactionStrategy instead? What is the difference?
> >
> > Anything else I should check?
> >
> > Lucian
> >
> > ________________________________________
> > From: Gerhard Petracek <gp...@apache.org>
> > Sent: Thursday, 7 May, 2020 11:58 AM
> > To: users@deltaspike.apache.org
> > Subject: Re: TransactionRequiredException when a transaction should exist
> >
> > hi lucian,
> >
> > please ensure that you are using the correct setup for ee-servers
> > (esp. see [1] and [2]).
> >
> > regards,
> > gerhard
> >
> > [1]
> >
> http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
> > [2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport
> >
> >
> >
> > Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
> > <Lu...@indsoft.ro>:
> > >
> > > Hi,
> > >
> > > I am using Deltaspike 1.9.3 in Wildfly 14.
> > >
> > > I have a CDI bean with a method that is supposed to insert or update an
> > entity, like this:
> > >
> > > @Named
> > > @ApplicationScoped
> > > public class StuffService {
> > >
> > >     @Inject
> > >     private StuffRepository repository; // StuffRepository extends
> > EntityRepository<Stuff, Long>
> > >
> > > ...
> > >
> > >     @Transactional
> > >     public void save(Stuff stuff) throws Exception {
> > >
> > >         if (stuff.getCode() == null) {
> > >             repository.save(stuff);
> > >
> > >         } else {
> > >
> > >             Stuff entity = getByCode(stuff.getCode()); // code is @Id
> > >             entity.setName(stuff.getName());
> > >             repository.save(entity);
> > >         }
> > >     }
> > >
> > > This works, but looking at the EntityRepository.save javadoc, I
> > understand that it already does what I want, decide if to update or
> insert
> > a new record based on the presence/absence of a primary key value.
> > >
> > > However, if I call repository.save() on an existing entity (so I remove
> > the if and always go in the first branch), I get an exception:
> > >
> > > javax.persistence.TransactionRequiredException: WFLYJPA0060:
> Transaction
> > is required to perform this operation (either use a transaction or
> extended
> > persistence context)
> > >     at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> > >     at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> > >
> > > What happens here? How come a transaction does not exist unless I
> create
> > a new object and transfer the data into it?
> > >
> > > What I guess might be relevant is that the Stuff entity comes from a
> > ViewScoped bean, from a previous request.
> > >
> > > Note, I am using container-managed transactions, and
> > javax.transaction.Transactional. Should I use
> > org.apache.deltaspike.jpa.api.transaction.Transactional? What is the
> > difference?
> > >
> > > Thanks in advance,
> > >
> > > Lucian
> > >
> > >
> > >
> >
>

Re: TransactionRequiredException when a transaction should exist

Posted by Gilberto <gi...@gmail.com>.
> What I guess might be relevant is that the Stuff entity comes from a
ViewScoped bean, from a previous request.

> javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction
is required to perform this operation (either use a transaction or >
extended persistence context)

I think you need to take care about it [1] .
I my case (TomEE) I use EJB with Eclipselink and it takes care for me [2] -
I do not use @Transactional
Like:
@Stateless
public class EventoService {

    protected transient final Logger logger =
LoggerFactory.getLogger(this.getClass());

    @Inject
    private EventoRepository eventoRepo;

    @Inject
    private AuthorizationBean autorizationBen;

    public void setAutorizationBen(AuthorizationBean autorizationBen) {
        this.autorizationBen = autorizationBen;
    }

    public Evento save(Evento evento) {
        evento.setCreatedBy(autorizationBen.getUsuarioID());
        evento.setUpdatedBy(evento.getCreatedBy());
        evento.setDtCreatedBy(LocalDateTime.now());
        evento.setDtUpdatedBy(LocalDateTime.now());
        return eventoRepo.save(evento);
    }

    public Evento saveAndFlushAndRefresh(Evento evento) {
        evento.setUpdatedBy(autorizationBen.getUsuarioID());
        evento.setDtUpdatedBy(LocalDateTime.now());
        return eventoRepo.saveAndFlushAndRefresh(evento);
    }

    public void delete(Evento evento) {
        eventoRepo.attachAndRemove(evento);
    }

    public Evento findById(Long id) {
        return eventoRepo.findBy(id);
    }

    public List<Evento> findAll() {      return eventoRepo.findAll();}
...

Regards,

Gilberto


[1]
https://deltaspike.apache.org/documentation/jpa.html#ExtendedPersistenceContexts
[2] https://tomee.apache.org/examples-trunk/transaction-rollback/README.html

Em qui., 7 de mai. de 2020 às 10:10, Lucian BRANCOVEAN <
Lucian.BRANCOVEAN@indsoft.ro> escreveu:

> Ok, I've changed that to:
>
> @ApplicationScoped
> public class EntityManagerProducer {
>
>         @Produces
>         @Dependent
>         @PersistenceContext(unitName = "pu")
>         private EntityManager entityManager;
>
> It seems to make no difference. Other operations still work, the update
> still fails:
>
>  Operation failed: javax.persistence.TransactionRequiredException:
> WFLYJPA0060: Transaction is required to perform this operation (either use
> a transaction or extended persistence context)
>         at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
>         at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.EntityRepositoryHandler.save(EntityRepositoryHandler.java:77)
>         at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>         at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>         at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.invoke(DelegateQueryBuilder.java:130)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.execute(DelegateQueryBuilder.java:60)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.QueryBuilder.executeQuery(QueryBuilder.java:57)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$1.proceed(TransactionalQueryRunner.java:73)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy.execute(ContainerManagedTransactionStrategy.java:40)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeTransactional(TransactionalQueryRunner.java:68)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeQuery(TransactionalQueryRunner.java:50)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$Proxy$_$$_WeldClientProxy.executeQuery(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.process(QueryHandler.java:151)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:130)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.proceed(DeltaSpikeProxyInvocationHandler.java:97)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.invoke(DeltaSpikeProxyInvocationHandler.java:78)
>         at
> [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown
> Source)
>         at
> [..]-0.0.1-SNAPSHOT.war//[..]StuffRepository$$DSPartialBeanProxy.save(Unknown
> Source)
>
>
> Lucian
>
> ________________________________________
> From: Gilberto <gi...@gmail.com>
> Sent: Thursday, 7 May, 2020 3:45 PM
> To: users@deltaspike.apache.org
> Subject: Re: TransactionRequiredException when a transaction should exist
>
> @ApplicationScoped is for your class which has several producers
> (requested-scope I think).
>
> You need the simplest one:
>
> public class EntityManagerProducer {
>
>     @Produces
>     @Dependent
>     @PersistenceContext(unitName = "gacePU")
>     public EntityManager entityManager;
> }
>
> Em qui., 7 de mai. de 2020 às 08:58, Lucian BRANCOVEAN <
> Lucian.BRANCOVEAN@indsoft.ro> escreveu:
>
> > Hi,
> >
> > This is my EM producer:
> >
> >         @PersistenceContext(unitName = "pu")
> >         private EntityManager entityManager;
> >
> >         @ApplicationScoped
> >         @Produces
> >         public EntityManager getEntityManager() {
> >                 return entityManager;
> >         }
> >
> >
> > I don't have a @Disposes method because I am using container-managed
> > persistence and it results in errors complainig that I should not close
> the
> > container-managed EM manually. Am I wrong in having it
> application-scoped?
> >
> > I have a src/main/resources/META-INF/apache-deltaspike.properties file
> > with this:
> >
> >
> >
> globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> >
> > And I see this in the log:
> >  INFO
> > [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension] (MSC
> > service thread 1-2)
> >
> org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> > is configured as global-alternative
> >
> > This is what I understood to be required in my case. Should I use
> > BeanManagedUserTransactionStrategy instead? What is the difference?
> >
> > Anything else I should check?
> >
> > Lucian
> >
> > ________________________________________
> > From: Gerhard Petracek <gp...@apache.org>
> > Sent: Thursday, 7 May, 2020 11:58 AM
> > To: users@deltaspike.apache.org
> > Subject: Re: TransactionRequiredException when a transaction should exist
> >
> > hi lucian,
> >
> > please ensure that you are using the correct setup for ee-servers
> > (esp. see [1] and [2]).
> >
> > regards,
> > gerhard
> >
> > [1]
> >
> http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
> > [2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport
> >
> >
> >
> > Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
> > <Lu...@indsoft.ro>:
> > >
> > > Hi,
> > >
> > > I am using Deltaspike 1.9.3 in Wildfly 14.
> > >
> > > I have a CDI bean with a method that is supposed to insert or update an
> > entity, like this:
> > >
> > > @Named
> > > @ApplicationScoped
> > > public class StuffService {
> > >
> > >     @Inject
> > >     private StuffRepository repository; // StuffRepository extends
> > EntityRepository<Stuff, Long>
> > >
> > > ...
> > >
> > >     @Transactional
> > >     public void save(Stuff stuff) throws Exception {
> > >
> > >         if (stuff.getCode() == null) {
> > >             repository.save(stuff);
> > >
> > >         } else {
> > >
> > >             Stuff entity = getByCode(stuff.getCode()); // code is @Id
> > >             entity.setName(stuff.getName());
> > >             repository.save(entity);
> > >         }
> > >     }
> > >
> > > This works, but looking at the EntityRepository.save javadoc, I
> > understand that it already does what I want, decide if to update or
> insert
> > a new record based on the presence/absence of a primary key value.
> > >
> > > However, if I call repository.save() on an existing entity (so I remove
> > the if and always go in the first branch), I get an exception:
> > >
> > > javax.persistence.TransactionRequiredException: WFLYJPA0060:
> Transaction
> > is required to perform this operation (either use a transaction or
> extended
> > persistence context)
> > >     at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> > >     at org.jboss.as.jpa@14.0.1.Final
> >
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> > >
> > > What happens here? How come a transaction does not exist unless I
> create
> > a new object and transfer the data into it?
> > >
> > > What I guess might be relevant is that the Stuff entity comes from a
> > ViewScoped bean, from a previous request.
> > >
> > > Note, I am using container-managed transactions, and
> > javax.transaction.Transactional. Should I use
> > org.apache.deltaspike.jpa.api.transaction.Transactional? What is the
> > difference?
> > >
> > > Thanks in advance,
> > >
> > > Lucian
> > >
> > >
> > >
> >
>

Re: TransactionRequiredException when a transaction should exist

Posted by Lucian BRANCOVEAN <Lu...@indsoft.ro>.
Ok, I've changed that to:

@ApplicationScoped
public class EntityManagerProducer {

	@Produces
	@Dependent
	@PersistenceContext(unitName = "pu")
	private EntityManager entityManager;

It seems to make no difference. Other operations still work, the update still fails:

 Operation failed: javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
	at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
	at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.EntityRepositoryHandler.save(EntityRepositoryHandler.java:77)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.invoke(DelegateQueryBuilder.java:130)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder.execute(DelegateQueryBuilder.java:60)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.QueryBuilder.executeQuery(QueryBuilder.java:57)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.builder.DelegateQueryBuilder$Proxy$_$$_WeldClientProxy.executeQuery(Unknown Source)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$1.proceed(TransactionalQueryRunner.java:73)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy.execute(ContainerManagedTransactionStrategy.java:40)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeTransactional(TransactionalQueryRunner.java:68)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeQuery(TransactionalQueryRunner.java:50)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner$Proxy$_$$_WeldClientProxy.executeQuery(Unknown Source)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.process(QueryHandler.java:151)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:130)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.data.impl.handler.QueryHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown Source)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.proceed(DeltaSpikeProxyInvocationHandler.java:97)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler.invoke(DeltaSpikeProxyInvocationHandler.java:78)
	at [..]-0.0.1-SNAPSHOT.war//org.apache.deltaspike.proxy.spi.invocation.DeltaSpikeProxyInvocationHandler$Proxy$_$$_WeldClientProxy.invoke(Unknown Source)
	at [..]-0.0.1-SNAPSHOT.war//[..]StuffRepository$$DSPartialBeanProxy.save(Unknown Source)
	
	
Lucian

________________________________________
From: Gilberto <gi...@gmail.com>
Sent: Thursday, 7 May, 2020 3:45 PM
To: users@deltaspike.apache.org
Subject: Re: TransactionRequiredException when a transaction should exist

@ApplicationScoped is for your class which has several producers
(requested-scope I think).

You need the simplest one:

public class EntityManagerProducer {

    @Produces
    @Dependent
    @PersistenceContext(unitName = "gacePU")
    public EntityManager entityManager;
}

Em qui., 7 de mai. de 2020 às 08:58, Lucian BRANCOVEAN <
Lucian.BRANCOVEAN@indsoft.ro> escreveu:

> Hi,
>
> This is my EM producer:
>
>         @PersistenceContext(unitName = "pu")
>         private EntityManager entityManager;
>
>         @ApplicationScoped
>         @Produces
>         public EntityManager getEntityManager() {
>                 return entityManager;
>         }
>
>
> I don't have a @Disposes method because I am using container-managed
> persistence and it results in errors complainig that I should not close the
> container-managed EM manually. Am I wrong in having it application-scoped?
>
> I have a src/main/resources/META-INF/apache-deltaspike.properties file
> with this:
>
>
> globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
>
> And I see this in the log:
>  INFO
> [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension] (MSC
> service thread 1-2)
> org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> is configured as global-alternative
>
> This is what I understood to be required in my case. Should I use
> BeanManagedUserTransactionStrategy instead? What is the difference?
>
> Anything else I should check?
>
> Lucian
>
> ________________________________________
> From: Gerhard Petracek <gp...@apache.org>
> Sent: Thursday, 7 May, 2020 11:58 AM
> To: users@deltaspike.apache.org
> Subject: Re: TransactionRequiredException when a transaction should exist
>
> hi lucian,
>
> please ensure that you are using the correct setup for ee-servers
> (esp. see [1] and [2]).
>
> regards,
> gerhard
>
> [1]
> http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
> [2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport
>
>
>
> Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
> <Lu...@indsoft.ro>:
> >
> > Hi,
> >
> > I am using Deltaspike 1.9.3 in Wildfly 14.
> >
> > I have a CDI bean with a method that is supposed to insert or update an
> entity, like this:
> >
> > @Named
> > @ApplicationScoped
> > public class StuffService {
> >
> >     @Inject
> >     private StuffRepository repository; // StuffRepository extends
> EntityRepository<Stuff, Long>
> >
> > ...
> >
> >     @Transactional
> >     public void save(Stuff stuff) throws Exception {
> >
> >         if (stuff.getCode() == null) {
> >             repository.save(stuff);
> >
> >         } else {
> >
> >             Stuff entity = getByCode(stuff.getCode()); // code is @Id
> >             entity.setName(stuff.getName());
> >             repository.save(entity);
> >         }
> >     }
> >
> > This works, but looking at the EntityRepository.save javadoc, I
> understand that it already does what I want, decide if to update or insert
> a new record based on the presence/absence of a primary key value.
> >
> > However, if I call repository.save() on an existing entity (so I remove
> the if and always go in the first branch), I get an exception:
> >
> > javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction
> is required to perform this operation (either use a transaction or extended
> persistence context)
> >     at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> >     at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> >
> > What happens here? How come a transaction does not exist unless I create
> a new object and transfer the data into it?
> >
> > What I guess might be relevant is that the Stuff entity comes from a
> ViewScoped bean, from a previous request.
> >
> > Note, I am using container-managed transactions, and
> javax.transaction.Transactional. Should I use
> org.apache.deltaspike.jpa.api.transaction.Transactional? What is the
> difference?
> >
> > Thanks in advance,
> >
> > Lucian
> >
> >
> >
>

Re: TransactionRequiredException when a transaction should exist

Posted by Gilberto <gi...@gmail.com>.
@ApplicationScoped is for your class which has several producers
(requested-scope I think).

You need the simplest one:

public class EntityManagerProducer {

    @Produces
    @Dependent
    @PersistenceContext(unitName = "gacePU")
    public EntityManager entityManager;
}

Em qui., 7 de mai. de 2020 às 08:58, Lucian BRANCOVEAN <
Lucian.BRANCOVEAN@indsoft.ro> escreveu:

> Hi,
>
> This is my EM producer:
>
>         @PersistenceContext(unitName = "pu")
>         private EntityManager entityManager;
>
>         @ApplicationScoped
>         @Produces
>         public EntityManager getEntityManager() {
>                 return entityManager;
>         }
>
>
> I don't have a @Disposes method because I am using container-managed
> persistence and it results in errors complainig that I should not close the
> container-managed EM manually. Am I wrong in having it application-scoped?
>
> I have a src/main/resources/META-INF/apache-deltaspike.properties file
> with this:
>
>
> globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
>
> And I see this in the log:
>  INFO
> [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension] (MSC
> service thread 1-2)
> org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy
> is configured as global-alternative
>
> This is what I understood to be required in my case. Should I use
> BeanManagedUserTransactionStrategy instead? What is the difference?
>
> Anything else I should check?
>
> Lucian
>
> ________________________________________
> From: Gerhard Petracek <gp...@apache.org>
> Sent: Thursday, 7 May, 2020 11:58 AM
> To: users@deltaspike.apache.org
> Subject: Re: TransactionRequiredException when a transaction should exist
>
> hi lucian,
>
> please ensure that you are using the correct setup for ee-servers
> (esp. see [1] and [2]).
>
> regards,
> gerhard
>
> [1]
> http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
> [2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport
>
>
>
> Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
> <Lu...@indsoft.ro>:
> >
> > Hi,
> >
> > I am using Deltaspike 1.9.3 in Wildfly 14.
> >
> > I have a CDI bean with a method that is supposed to insert or update an
> entity, like this:
> >
> > @Named
> > @ApplicationScoped
> > public class StuffService {
> >
> >     @Inject
> >     private StuffRepository repository; // StuffRepository extends
> EntityRepository<Stuff, Long>
> >
> > ...
> >
> >     @Transactional
> >     public void save(Stuff stuff) throws Exception {
> >
> >         if (stuff.getCode() == null) {
> >             repository.save(stuff);
> >
> >         } else {
> >
> >             Stuff entity = getByCode(stuff.getCode()); // code is @Id
> >             entity.setName(stuff.getName());
> >             repository.save(entity);
> >         }
> >     }
> >
> > This works, but looking at the EntityRepository.save javadoc, I
> understand that it already does what I want, decide if to update or insert
> a new record based on the presence/absence of a primary key value.
> >
> > However, if I call repository.save() on an existing entity (so I remove
> the if and always go in the first branch), I get an exception:
> >
> > javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction
> is required to perform this operation (either use a transaction or extended
> persistence context)
> >     at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
> >     at org.jboss.as.jpa@14.0.1.Final
> //org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
> >
> > What happens here? How come a transaction does not exist unless I create
> a new object and transfer the data into it?
> >
> > What I guess might be relevant is that the Stuff entity comes from a
> ViewScoped bean, from a previous request.
> >
> > Note, I am using container-managed transactions, and
> javax.transaction.Transactional. Should I use
> org.apache.deltaspike.jpa.api.transaction.Transactional? What is the
> difference?
> >
> > Thanks in advance,
> >
> > Lucian
> >
> >
> >
>

Re: TransactionRequiredException when a transaction should exist

Posted by Lucian BRANCOVEAN <Lu...@indsoft.ro>.
Hi,

This is my EM producer:

	@PersistenceContext(unitName = "pu")
	private EntityManager entityManager;

	@ApplicationScoped
	@Produces
	public EntityManager getEntityManager() {
		return entityManager;
	}


I don't have a @Disposes method because I am using container-managed persistence and it results in errors complainig that I should not close the container-managed EM manually. Am I wrong in having it application-scoped?

I have a src/main/resources/META-INF/apache-deltaspike.properties file with this:

globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy

And I see this in the log:
 INFO  [org.apache.deltaspike.core.impl.exclude.extension.ExcludeExtension] (MSC service thread 1-2) org.apache.deltaspike.jpa.impl.transaction.ContainerManagedTransactionStrategy is configured as global-alternative

This is what I understood to be required in my case. Should I use BeanManagedUserTransactionStrategy instead? What is the difference?

Anything else I should check?

Lucian

________________________________________
From: Gerhard Petracek <gp...@apache.org>
Sent: Thursday, 7 May, 2020 11:58 AM
To: users@deltaspike.apache.org
Subject: Re: TransactionRequiredException when a transaction should exist

hi lucian,

please ensure that you are using the correct setup for ee-servers
(esp. see [1] and [2]).

regards,
gerhard

[1] http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
[2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport



Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
<Lu...@indsoft.ro>:
>
> Hi,
>
> I am using Deltaspike 1.9.3 in Wildfly 14.
>
> I have a CDI bean with a method that is supposed to insert or update an entity, like this:
>
> @Named
> @ApplicationScoped
> public class StuffService {
>
>     @Inject
>     private StuffRepository repository; // StuffRepository extends EntityRepository<Stuff, Long>
>
> ...
>
>     @Transactional
>     public void save(Stuff stuff) throws Exception {
>
>         if (stuff.getCode() == null) {
>             repository.save(stuff);
>
>         } else {
>
>             Stuff entity = getByCode(stuff.getCode()); // code is @Id
>             entity.setName(stuff.getName());
>             repository.save(entity);
>         }
>     }
>
> This works, but looking at the EntityRepository.save javadoc, I understand that it already does what I want, decide if to update or insert a new record based on the presence/absence of a primary key value.
>
> However, if I call repository.save() on an existing entity (so I remove the if and always go in the first branch), I get an exception:
>
> javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
>     at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
>     at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
>
> What happens here? How come a transaction does not exist unless I create a new object and transfer the data into it?
>
> What I guess might be relevant is that the Stuff entity comes from a ViewScoped bean, from a previous request.
>
> Note, I am using container-managed transactions, and javax.transaction.Transactional. Should I use org.apache.deltaspike.jpa.api.transaction.Transactional? What is the difference?
>
> Thanks in advance,
>
> Lucian
>
>
>

Re: TransactionRequiredException when a transaction should exist

Posted by Gerhard Petracek <gp...@apache.org>.
hi lucian,

please ensure that you are using the correct setup for ee-servers
(esp. see [1] and [2]).

regards,
gerhard

[1] http://deltaspike.apache.org/documentation/data.html#3.CompleteAdditionalProjectConfiguration
[2] http://deltaspike.apache.org/documentation/jpa.html#JTASupport



Am Mo., 4. Mai 2020 um 22:24 Uhr schrieb Lucian BRANCOVEAN
<Lu...@indsoft.ro>:
>
> Hi,
>
> I am using Deltaspike 1.9.3 in Wildfly 14.
>
> I have a CDI bean with a method that is supposed to insert or update an entity, like this:
>
> @Named
> @ApplicationScoped
> public class StuffService {
>
>     @Inject
>     private StuffRepository repository; // StuffRepository extends EntityRepository<Stuff, Long>
>
> ...
>
>     @Transactional
>     public void save(Stuff stuff) throws Exception {
>
>         if (stuff.getCode() == null) {
>             repository.save(stuff);
>
>         } else {
>
>             Stuff entity = getByCode(stuff.getCode()); // code is @Id
>             entity.setName(stuff.getName());
>             repository.save(entity);
>         }
>     }
>
> This works, but looking at the EntityRepository.save javadoc, I understand that it already does what I want, decide if to update or insert a new record based on the presence/absence of a primary key value.
>
> However, if I call repository.save() on an existing entity (so I remove the if and always go in the first branch), I get an exception:
>
> javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
>     at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:877)
>     at org.jboss.as.jpa@14.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.merge(AbstractEntityManager.java:564)
>
> What happens here? How come a transaction does not exist unless I create a new object and transfer the data into it?
>
> What I guess might be relevant is that the Stuff entity comes from a ViewScoped bean, from a previous request.
>
> Note, I am using container-managed transactions, and javax.transaction.Transactional. Should I use org.apache.deltaspike.jpa.api.transaction.Transactional? What is the difference?
>
> Thanks in advance,
>
> Lucian
>
>
>