Singletone bean, concurrency and transactions

Moderator: admin

Post Reply
alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Singletone bean, concurrency and transactions

Post by alex »

Hi Paul,

I've passed an exam today 95% and found a new question regarding Singletone bean.
Could you please explain how does work the concurrency and transactions together for singletone bean.

The question was following:
We have an Singletone bean and two threads try to access firstMethod() simultaneously. How many transactions will be created?
Answers: 0 or 1 or 2

@Singleton
@Lock(LockType.READ)
public class PoJo {
public void firstMethod () { ... }
public void secondMethod () { ... } // Lock (Read) class level
}

Thanks in advance,
Alex

Online
admin
Site Admin
Posts: 10060
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by admin »

Hi Alex,
Congratulations!!

There are a couple of things here -

1. Since the bean uses @Lock and does not specify concurrency management type, it means it uses container managed concurrency management.
2. There is no lock type on firstMethod. So WRITE must be assumed because that is the default.

Now, a container cannot run two threads simulatenous of a method with WRITE lock. So the method execution will have to be serialized.

Further, as per section 13.3.3,
A singleton session bean instance must commit a transaction before a business method or timeout callback method or PostConstruct/PreDestroy lifecycle callback interceptor method returns.
Therefore, a new transaction will be created for each execution of firstMethod.

Thank you for getting back to us with this question. We will add a few questions to cover this.


HTH,
Paul.
If you like our products and services, please help us by posting your review here.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by alex »

Hi,

Thank you for answer.
What about @Lock(LockType.READ). As it was declared at type level the LockType.READ have to be assigned to all methods (if no override).
How does it resolve transaction in this case?

Online
admin
Site Admin
Posts: 10060
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by admin »

Oh, sorry, I didn't realize it was on class. In that case, it should apply to all the methods and the container will let multiple threads on the method simultaneously.

However, that still doesn't change the answer (2 transactions) because a transaction context is associated with a thread.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by alex »

Great, thank you.

Online
admin
Site Admin
Posts: 10060
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by admin »

Can you think of anything else that you got on the exam but was not covered well in our questions?

Overall, did you find our content useful for the exam?

thank you,
Paul.
If you like our products and services, please help us by posting your review here.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by alex »

I've answered 57/60 questions.
So I believe that you guys have done a great job.
My week area was: Create interceptors for an EJB 2/4. But I thought that I am ok with it.

There was a question about overriding interceptor's order.
It is given 3 interceptor classes A, B, C.
B and C are declared at type level. @Interceptors({B, C});
interceptor-A didn't declared via annotations.
And we have a following descriptor:
<assembly-descriptor>
<interceptor-binding>
<ejb-name>SomeBean</ejb-name>
<interceptor-order>
<interceptor-class>A</interceptor-class>
</interceptor-order>
<method>
<method-name>foo</method-name>
</method>
</interceptor-binding>
</assembly-descriptor>

What will be the order for invocations?
1. B, C
2. A, B, C
3. B, C, A
4. A

What annotations we can use for field, method and type. (easy one but don't remember in mocks)
@Resource
@Init
@EJB
@RollesAllowed


Some interesting questions about transactions.
You have a methodA. methodA calls consequentially method1, method2, method3, method4.
method2 should have as fast transaction as it can.
Couple of options:
methodA - Required (Not supporetd, ..)
method1 - Required
method2 - Required New
method3 - Mandatory
method4 - Not supported
Or all methods are required...

Next.
You have to work with 2 databases. 1 - SQL base that do support transatios, 2 - LDAP that doesn't support transactions/
You have to update each base in method foo(). If either of them has not updated properly you have to roll back all data.
Options like:
1. foo method with CMT (REquired). Update SQL, listen to errors, if errors throw exception, then do LDAP and listen exceptions
2. foo method with CMT (REquired). Update LDAP, and listen excep, if any ctx.setRollBackOnly, then update SQL
3. foo method CMT (NotSupported)
4. BMT..

I am looking forward to see Web Services mocks ;)

BR,
Alex

Online
admin
Site Admin
Posts: 10060
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Singletone bean, concurrency and transactions

Post by admin »

Thanks a lot, Alex. We will work on improving in these topics.

-Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 73 guests