deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lucian BRANCOVEAN <Lucian.BRANCOV...@indsoft.ro>
Subject Re: TransactionRequiredException when a transaction should exist
Date Thu, 07 May 2020 11:58:39 GMT
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 <gpetracek@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
<Lucian.BRANCOVEAN@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
>
>
>

Mime
View raw message