You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by José Luis Cetina <ma...@gmail.com> on 2013/09/06 02:09:36 UTC

TransactionAttribute

Hi i have an EJB annotated with:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)

As you can see i don't mark the EJB with any TransactionAttribute, instead
i use
TransactionAttribute in every method when i need it, if i don't need any
transaction (because i will only read information from database) then i
don't use any TransactionAttribute in the method not event
TransactionAttribute.NEVER.

But today i realized that i have a method that IS NOT annotated with
any TransactionAttribute (and the ejb class either) but in that method i
query an entity and change some values  (inside of the ejb method) of the
retrieved entity, i could see that my entity is committed with those
changes (this is not suppose to happens).

I know if i mark the EJB with TransactionAttribute.XXX all the methods will
use the same TransactionAttribute that is defined in the class, i know if i
have the TransactionAttribute in the ejb class and in the method too, the
method TransactionAttribute has major priority. But in my case
i don't have any TransactionAttribute in the ejb class and in the method.

Then why my entity is commited with those changes?, is the default value
for TransactionAttribute using TransactionManagementType.CONTAINER
is @TransactionAttribute(TransactionAttributeType.REQUIRED) or something
like that??

If i annotated my method
with @TransactionAttribute(TransactionAttributeType.NEVER) the entity is
never persisted (required behavior), but if i add
the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
entity is persisted.

My surprise is if i use
@TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
persisted but i never do a persist or merge, i know if the method never
throw an exception the transaction is commited, but i never do a
persist/merge.

Do i have to mark all of my methods with
@TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i can
assume that the default value for an EJB is @TransactionAttribute.REQUIRED??

Re: TransactionAttribute

Posted by Anthony Fryer <ap...@hotmail.com>.
See http://tomee.apache.org/transaction-annotations.html



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

Re: TransactionAttribute

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

default is REQUIRED, you can override this default by class (putting it on
the class instead the method)

the entity is commited cause you did a find on it because (i guess) and
then it is attached, merge is optional in this case while you are in a
transaction.


*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/9/6 José Luis Cetina <ma...@gmail.com>

> Hi i have an EJB annotated with:
>
> @Stateless
> @TransactionManagement(TransactionManagementType.CONTAINER)
>
> As you can see i don't mark the EJB with any TransactionAttribute, instead
> i use
> TransactionAttribute in every method when i need it, if i don't need any
> transaction (because i will only read information from database) then i
> don't use any TransactionAttribute in the method not event
> TransactionAttribute.NEVER.
>
> But today i realized that i have a method that IS NOT annotated with
> any TransactionAttribute (and the ejb class either) but in that method i
> query an entity and change some values  (inside of the ejb method) of the
> retrieved entity, i could see that my entity is committed with those
> changes (this is not suppose to happens).
>
> I know if i mark the EJB with TransactionAttribute.XXX all the methods will
> use the same TransactionAttribute that is defined in the class, i know if i
> have the TransactionAttribute in the ejb class and in the method too, the
> method TransactionAttribute has major priority. But in my case
> i don't have any TransactionAttribute in the ejb class and in the method.
>
> Then why my entity is commited with those changes?, is the default value
> for TransactionAttribute using TransactionManagementType.CONTAINER
> is @TransactionAttribute(TransactionAttributeType.REQUIRED) or something
> like that??
>
> If i annotated my method
> with @TransactionAttribute(TransactionAttributeType.NEVER) the entity is
> never persisted (required behavior), but if i add
> the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
> entity is persisted.
>
> My surprise is if i use
> @TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
> persisted but i never do a persist or merge, i know if the method never
> throw an exception the transaction is commited, but i never do a
> persist/merge.
>
> Do i have to mark all of my methods with
> @TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i can
> assume that the default value for an EJB is
> @TransactionAttribute.REQUIRED??
>

Re: TransactionAttribute

Posted by José Luis Cetina <ma...@gmail.com>.
I fixed using @TransactionAttribute(TransactionAttributeType.REQUIRED) in
the class (the default) and then an each method if i dont need a
transaction i use @TransactionAttribute(TransactionAttributeType.NEVER)





2013/9/6 José Luis Cetina <ma...@gmail.com>

> Hi, i have a problem with this.
>
> My EJB is annotated with TransactionAttributeType.NEVER and
> TransactionManagementType.CONTAINER, my ejb method use
> TransactionAttributeType.REQUIRED, my idea is that every method when need a
> transaction they use TransactionAttributeType.REQUIRED and then persist,
> but im getting a TransactionRequiredException. If my method says required
> why i got that exception?
>
> Example:
>
> @Stateless
> @TransactionAttribute(TransactionAttributeType.NEVER)
> @TransactionManagement(TransactionManagementType.CONTAINER)
> public class MyEJB{
>
> @TransactionAttribute(TransactionAttributeType.REQUIRED)
> public void myMethod(MyClass obj){
>    entityManager.persist(obj);
>   }
> }
>
>
> Log:
> SEVERE - EjbTransactionUtil.handleSystemException: null
> javax.persistence.TransactionRequiredException
> at
> org.apache.openejb.persistence.JtaEntityManager.assertTransactionActive(JtaEntityManager.java:105)
>  at
> org.apache.openejb.persistence.JtaEntityManager.persist(JtaEntityManager.java:134)
>
>
>
> 2013/9/5 José Luis Cetina <ma...@gmail.com>
>
>> Ok, understood. Thanks
>>
>>
>> 2013/9/5 John D. Ament <jo...@gmail.com>
>>
>> From the java ee 5 tutorial (don't think this changed in 6):
>>> http://docs.oracle.com/javaee/5/tutorial/doc/bncij.html
>>>
>>> Required Attribute
>>>
>>> If the client is running within a transaction and invokes the
>>> enterprise bean’s method, the method executes within the client’s
>>> transaction. If the client is not associated with a transaction, the
>>> container starts a new transaction before running the method.
>>>
>>> The Required attribute is the implicit transaction attribute for all
>>> enterprise bean methods running with container-managed transaction
>>> demarcation. You typically do not set the Required attribute unless
>>> you need to override another transaction attribute. Because
>>> transaction attributes are declarative, you can easily change them
>>> later.
>>>
>>> John
>>>
>>> On Thu, Sep 5, 2013 at 8:09 PM, José Luis Cetina <ma...@gmail.com>
>>> wrote:
>>> > Hi i have an EJB annotated with:
>>> >
>>> > @Stateless
>>> > @TransactionManagement(TransactionManagementType.CONTAINER)
>>> >
>>> > As you can see i don't mark the EJB with any TransactionAttribute,
>>> instead
>>> > i use
>>> > TransactionAttribute in every method when i need it, if i don't need
>>> any
>>> > transaction (because i will only read information from database) then i
>>> > don't use any TransactionAttribute in the method not event
>>> > TransactionAttribute.NEVER.
>>> >
>>> > But today i realized that i have a method that IS NOT annotated with
>>> > any TransactionAttribute (and the ejb class either) but in that method
>>> i
>>> > query an entity and change some values  (inside of the ejb method) of
>>> the
>>> > retrieved entity, i could see that my entity is committed with those
>>> > changes (this is not suppose to happens).
>>> >
>>> > I know if i mark the EJB with TransactionAttribute.XXX all the methods
>>> will
>>> > use the same TransactionAttribute that is defined in the class, i know
>>> if i
>>> > have the TransactionAttribute in the ejb class and in the method too,
>>> the
>>> > method TransactionAttribute has major priority. But in my case
>>> > i don't have any TransactionAttribute in the ejb class and in the
>>> method.
>>> >
>>> > Then why my entity is commited with those changes?, is the default
>>> value
>>> > for TransactionAttribute using TransactionManagementType.CONTAINER
>>> > is @TransactionAttribute(TransactionAttributeType.REQUIRED) or
>>> something
>>> > like that??
>>> >
>>> > If i annotated my method
>>> > with @TransactionAttribute(TransactionAttributeType.NEVER) the entity
>>> is
>>> > never persisted (required behavior), but if i add
>>> > the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
>>> > entity is persisted.
>>> >
>>> > My surprise is if i use
>>> > @TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
>>> > persisted but i never do a persist or merge, i know if the method never
>>> > throw an exception the transaction is commited, but i never do a
>>> > persist/merge.
>>> >
>>> > Do i have to mark all of my methods with
>>> > @TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i
>>> can
>>> > assume that the default value for an EJB is
>>> @TransactionAttribute.REQUIRED??
>>>
>>
>>
>>
>> --
>> -------------------------------------------------------------------
>> *SCJA. José Luis Cetina*
>> -------------------------------------------------------------------
>>
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: TransactionAttribute

Posted by José Luis Cetina <ma...@gmail.com>.
Hi, i have a problem with this.

My EJB is annotated with TransactionAttributeType.NEVER and
TransactionManagementType.CONTAINER, my ejb method use
TransactionAttributeType.REQUIRED, my idea is that every method when need a
transaction they use TransactionAttributeType.REQUIRED and then persist,
but im getting a TransactionRequiredException. If my method says required
why i got that exception?

Example:

@Stateless
@TransactionAttribute(TransactionAttributeType.NEVER)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyEJB{

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void myMethod(MyClass obj){
   entityManager.persist(obj);
  }
}


Log:
SEVERE - EjbTransactionUtil.handleSystemException: null
javax.persistence.TransactionRequiredException
at
org.apache.openejb.persistence.JtaEntityManager.assertTransactionActive(JtaEntityManager.java:105)
at
org.apache.openejb.persistence.JtaEntityManager.persist(JtaEntityManager.java:134)



2013/9/5 José Luis Cetina <ma...@gmail.com>

> Ok, understood. Thanks
>
>
> 2013/9/5 John D. Ament <jo...@gmail.com>
>
> From the java ee 5 tutorial (don't think this changed in 6):
>> http://docs.oracle.com/javaee/5/tutorial/doc/bncij.html
>>
>> Required Attribute
>>
>> If the client is running within a transaction and invokes the
>> enterprise bean’s method, the method executes within the client’s
>> transaction. If the client is not associated with a transaction, the
>> container starts a new transaction before running the method.
>>
>> The Required attribute is the implicit transaction attribute for all
>> enterprise bean methods running with container-managed transaction
>> demarcation. You typically do not set the Required attribute unless
>> you need to override another transaction attribute. Because
>> transaction attributes are declarative, you can easily change them
>> later.
>>
>> John
>>
>> On Thu, Sep 5, 2013 at 8:09 PM, José Luis Cetina <ma...@gmail.com>
>> wrote:
>> > Hi i have an EJB annotated with:
>> >
>> > @Stateless
>> > @TransactionManagement(TransactionManagementType.CONTAINER)
>> >
>> > As you can see i don't mark the EJB with any TransactionAttribute,
>> instead
>> > i use
>> > TransactionAttribute in every method when i need it, if i don't need any
>> > transaction (because i will only read information from database) then i
>> > don't use any TransactionAttribute in the method not event
>> > TransactionAttribute.NEVER.
>> >
>> > But today i realized that i have a method that IS NOT annotated with
>> > any TransactionAttribute (and the ejb class either) but in that method i
>> > query an entity and change some values  (inside of the ejb method) of
>> the
>> > retrieved entity, i could see that my entity is committed with those
>> > changes (this is not suppose to happens).
>> >
>> > I know if i mark the EJB with TransactionAttribute.XXX all the methods
>> will
>> > use the same TransactionAttribute that is defined in the class, i know
>> if i
>> > have the TransactionAttribute in the ejb class and in the method too,
>> the
>> > method TransactionAttribute has major priority. But in my case
>> > i don't have any TransactionAttribute in the ejb class and in the
>> method.
>> >
>> > Then why my entity is commited with those changes?, is the default value
>> > for TransactionAttribute using TransactionManagementType.CONTAINER
>> > is @TransactionAttribute(TransactionAttributeType.REQUIRED) or something
>> > like that??
>> >
>> > If i annotated my method
>> > with @TransactionAttribute(TransactionAttributeType.NEVER) the entity is
>> > never persisted (required behavior), but if i add
>> > the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
>> > entity is persisted.
>> >
>> > My surprise is if i use
>> > @TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
>> > persisted but i never do a persist or merge, i know if the method never
>> > throw an exception the transaction is commited, but i never do a
>> > persist/merge.
>> >
>> > Do i have to mark all of my methods with
>> > @TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i
>> can
>> > assume that the default value for an EJB is
>> @TransactionAttribute.REQUIRED??
>>
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: TransactionAttribute

Posted by José Luis Cetina <ma...@gmail.com>.
Ok, understood. Thanks


2013/9/5 John D. Ament <jo...@gmail.com>

> From the java ee 5 tutorial (don't think this changed in 6):
> http://docs.oracle.com/javaee/5/tutorial/doc/bncij.html
>
> Required Attribute
>
> If the client is running within a transaction and invokes the
> enterprise bean’s method, the method executes within the client’s
> transaction. If the client is not associated with a transaction, the
> container starts a new transaction before running the method.
>
> The Required attribute is the implicit transaction attribute for all
> enterprise bean methods running with container-managed transaction
> demarcation. You typically do not set the Required attribute unless
> you need to override another transaction attribute. Because
> transaction attributes are declarative, you can easily change them
> later.
>
> John
>
> On Thu, Sep 5, 2013 at 8:09 PM, José Luis Cetina <ma...@gmail.com>
> wrote:
> > Hi i have an EJB annotated with:
> >
> > @Stateless
> > @TransactionManagement(TransactionManagementType.CONTAINER)
> >
> > As you can see i don't mark the EJB with any TransactionAttribute,
> instead
> > i use
> > TransactionAttribute in every method when i need it, if i don't need any
> > transaction (because i will only read information from database) then i
> > don't use any TransactionAttribute in the method not event
> > TransactionAttribute.NEVER.
> >
> > But today i realized that i have a method that IS NOT annotated with
> > any TransactionAttribute (and the ejb class either) but in that method i
> > query an entity and change some values  (inside of the ejb method) of the
> > retrieved entity, i could see that my entity is committed with those
> > changes (this is not suppose to happens).
> >
> > I know if i mark the EJB with TransactionAttribute.XXX all the methods
> will
> > use the same TransactionAttribute that is defined in the class, i know
> if i
> > have the TransactionAttribute in the ejb class and in the method too, the
> > method TransactionAttribute has major priority. But in my case
> > i don't have any TransactionAttribute in the ejb class and in the method.
> >
> > Then why my entity is commited with those changes?, is the default value
> > for TransactionAttribute using TransactionManagementType.CONTAINER
> > is @TransactionAttribute(TransactionAttributeType.REQUIRED) or something
> > like that??
> >
> > If i annotated my method
> > with @TransactionAttribute(TransactionAttributeType.NEVER) the entity is
> > never persisted (required behavior), but if i add
> > the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
> > entity is persisted.
> >
> > My surprise is if i use
> > @TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
> > persisted but i never do a persist or merge, i know if the method never
> > throw an exception the transaction is commited, but i never do a
> > persist/merge.
> >
> > Do i have to mark all of my methods with
> > @TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i
> can
> > assume that the default value for an EJB is
> @TransactionAttribute.REQUIRED??
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: TransactionAttribute

Posted by "John D. Ament" <jo...@gmail.com>.
>From the java ee 5 tutorial (don't think this changed in 6):
http://docs.oracle.com/javaee/5/tutorial/doc/bncij.html

Required Attribute

If the client is running within a transaction and invokes the
enterprise bean’s method, the method executes within the client’s
transaction. If the client is not associated with a transaction, the
container starts a new transaction before running the method.

The Required attribute is the implicit transaction attribute for all
enterprise bean methods running with container-managed transaction
demarcation. You typically do not set the Required attribute unless
you need to override another transaction attribute. Because
transaction attributes are declarative, you can easily change them
later.

John

On Thu, Sep 5, 2013 at 8:09 PM, José Luis Cetina <ma...@gmail.com> wrote:
> Hi i have an EJB annotated with:
>
> @Stateless
> @TransactionManagement(TransactionManagementType.CONTAINER)
>
> As you can see i don't mark the EJB with any TransactionAttribute, instead
> i use
> TransactionAttribute in every method when i need it, if i don't need any
> transaction (because i will only read information from database) then i
> don't use any TransactionAttribute in the method not event
> TransactionAttribute.NEVER.
>
> But today i realized that i have a method that IS NOT annotated with
> any TransactionAttribute (and the ejb class either) but in that method i
> query an entity and change some values  (inside of the ejb method) of the
> retrieved entity, i could see that my entity is committed with those
> changes (this is not suppose to happens).
>
> I know if i mark the EJB with TransactionAttribute.XXX all the methods will
> use the same TransactionAttribute that is defined in the class, i know if i
> have the TransactionAttribute in the ejb class and in the method too, the
> method TransactionAttribute has major priority. But in my case
> i don't have any TransactionAttribute in the ejb class and in the method.
>
> Then why my entity is commited with those changes?, is the default value
> for TransactionAttribute using TransactionManagementType.CONTAINER
> is @TransactionAttribute(TransactionAttributeType.REQUIRED) or something
> like that??
>
> If i annotated my method
> with @TransactionAttribute(TransactionAttributeType.NEVER) the entity is
> never persisted (required behavior), but if i add
> the @TransactionAttribute(TransactionAttributeType.REQUIRED) then the
> entity is persisted.
>
> My surprise is if i use
> @TransactionAttribute(TransactionAttributeType.REQUIRED) the entity is
> persisted but i never do a persist or merge, i know if the method never
> throw an exception the transaction is commited, but i never do a
> persist/merge.
>
> Do i have to mark all of my methods with
> @TransactionAttribute(TransactionAttributeType.NEVER)?? if yes, then i can
> assume that the default value for an EJB is @TransactionAttribute.REQUIRED??

Re: TransactionAttribute

Posted by Anthony Fryer <ap...@hotmail.com>.
I think it is because TransactionAttributeType.REQUIRED is the default used
if you don't specify anything.



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