You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by zeeman <ha...@fastmail.us> on 2013/07/01 06:04:57 UTC

@Asynchronous And TransactionRequiredException

Hi, 

I have the following 2 classes. I get TransactionRequiredException  only
when I have @Asynchronous on stateless EJB. If I remove the annotation,
everything works. I'm on Tomee 1.6 snapshot from a couple of weeks ago. 

Is this related to a bug in @Asynchronous? Or because I load info object in
a non-EJB class, then try to use it in stateless EJB? Or something else?


@ViewScoped
@Named
public class EditInfoComponent{

       @Inject
       private AsyncQueryProcessor asyncQuery;

	@Inject
	private EntityManager entityManager;   

      @Transactional   //from Deltaspike
      public void handleChanges(){
             //get info from entityManager
            asyncQuery.sendNotifications(info);
     }
}


@Stateless
public class AsyncQueryProcessor implements Serializable {

        @PersistenceContext(unitName = PersistenceConfiguration.UNIT_NAME)
	private EntityManager entityManager;

         @Asynchronous       //works with this annotation removed
         public void sendNotifications(Info info){
           
            if (info.isNew()){
                  Notification notif = new Notification();
                  entityManager.persist(notif);
            }else{
                  Notification notif =
entityManager.find(Notification.class, info.getNotificationId());
                  notif.setSomeFlags(true);
                  entityManager.merge(notif); //get
TransactionRequiredException 
            }
         }
}

A class to produce EntityManager:

public class PersistenceConfiguration implements Serializable {
        @PersistenceUnit(unitName = UNIT_NAME)
	private EntityManagerFactory entityManagerFactory;

	@Produces
	@RequestScoped
	@Default
	public EntityManager createEntityManager() {
		return entityManagerFactory.createEntityManager();
	}

         public void closeEntityManager(@Disposes final EntityManager em) {
		if (em.isOpen()) 
		{
			em.close();
		}
	}
}



--
View this message in context: http://openejb.979440.n4.nabble.com/Asynchronous-And-TransactionRequiredException-tp4664007.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Asynchronous And TransactionRequiredException

Posted by zeeman <ha...@fastmail.us>.
It's JtaEntityManager instance.

I found the issue. As you see in code snippet, I was using a reference from
Deltaspike EM and setting it on an object from stateless EJB. In other
words, using an entity in an EM that did not load it. If I put all code
inside @async or remove @async it works, makes sense. 

I wish if entities from entityManager in EJBs or regular beans can see each
other's changes.

This time it's my fault :)  Thx for the help.



--
View this message in context: http://openejb.979440.n4.nabble.com/Asynchronous-And-TransactionRequiredException-tp4664007p4664035.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Asynchronous And TransactionRequiredException

Posted by Romain Manni-Bucau <rm...@gmail.com>.
you create yourself the em so i doubt it is a jta one, can you check the
instance you get injected is a JtaEntityManager or an openjpa one?

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*



2013/7/1 zeeman <ha...@fastmail.us>

> My EntityManager is a JTA one. I'll try to flush EM before calling async
> method to see if that helps.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Asynchronous-And-TransactionRequiredException-tp4664007p4664024.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: @Asynchronous And TransactionRequiredException

Posted by zeeman <ha...@fastmail.us>.
My EntityManager is a JTA one. I'll try to flush EM before calling async
method to see if that helps.



--
View this message in context: http://openejb.979440.n4.nabble.com/Asynchronous-And-TransactionRequiredException-tp4664007p4664024.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Asynchronous And TransactionRequiredException

Posted by Romain Manni-Bucau <rm...@gmail.com>.
@Async and tx works fine on trunk, your issue should be different, maybe
the fact your entitymanager is not a jta one.

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*



2013/7/1 zeeman <ha...@fastmail.us>

> Hi,
>
> I have the following 2 classes. I get TransactionRequiredException  only
> when I have @Asynchronous on stateless EJB. If I remove the annotation,
> everything works. I'm on Tomee 1.6 snapshot from a couple of weeks ago.
>
> Is this related to a bug in @Asynchronous? Or because I load info object in
> a non-EJB class, then try to use it in stateless EJB? Or something else?
>
>
> @ViewScoped
> @Named
> public class EditInfoComponent{
>
>        @Inject
>        private AsyncQueryProcessor asyncQuery;
>
>         @Inject
>         private EntityManager entityManager;
>
>       @Transactional   //from Deltaspike
>       public void handleChanges(){
>              //get info from entityManager
>             asyncQuery.sendNotifications(info);
>      }
> }
>
>
> @Stateless
> public class AsyncQueryProcessor implements Serializable {
>
>         @PersistenceContext(unitName = PersistenceConfiguration.UNIT_NAME)
>         private EntityManager entityManager;
>
>          @Asynchronous       //works with this annotation removed
>          public void sendNotifications(Info info){
>
>             if (info.isNew()){
>                   Notification notif = new Notification();
>                   entityManager.persist(notif);
>             }else{
>                   Notification notif =
> entityManager.find(Notification.class, info.getNotificationId());
>                   notif.setSomeFlags(true);
>                   entityManager.merge(notif); //get
> TransactionRequiredException
>             }
>          }
> }
>
> A class to produce EntityManager:
>
> public class PersistenceConfiguration implements Serializable {
>         @PersistenceUnit(unitName = UNIT_NAME)
>         private EntityManagerFactory entityManagerFactory;
>
>         @Produces
>         @RequestScoped
>         @Default
>         public EntityManager createEntityManager() {
>                 return entityManagerFactory.createEntityManager();
>         }
>
>          public void closeEntityManager(@Disposes final EntityManager em) {
>                 if (em.isOpen())
>                 {
>                         em.close();
>                 }
>         }
> }
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Asynchronous-And-TransactionRequiredException-tp4664007.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>