EJB and Synchronization
Asked Answered
E

5

6

Are Session Beans (stateless session beans, stateful session beans) Synchronized?

Equivocate answered 21/5, 2009 at 3:50 Comment(2)
somebody asked me this question, what should be the correct answer? I didn't see any synchronized methods inside of EJB, I would say EJB is not synchronized. Like people used to ask "is Hashtable synchronized?"Equivocate
Technically your question is not clear. Do you mean: Need Session beans to be made thread safe? or Are session beans thread-safe?Crowbar
Y
6

Only one thread at a time will be accessing your beans. It is up to the application server to manage this. So you should not be using synchronized from within your beans. This is why a non-threadsafe like EntityManager can be an instance value and not have synchronization issues.

Ypres answered 21/5, 2009 at 3:59 Comment(4)
Yeah thats fine, but somebody asked this question in a interview and I replied in the same way but he wasn't satisfied with my answer.Equivocate
It's always possible they didn't have a clue.Nadbus
Exactly. Further, the new @AccessTimeout annotation allows you to configure, in a portable way, exactly how long you want a calling thread to wait for an instance to become available. Also note that the new @Singleton bean can be configured to allow multithreaded access but is not so by default. That decision was made in efforts to keep consistency behind the default rules for all Session beans (@Stateless, @Stateful, @Singleton)Ambassadoratlarge
Executing the same code more than once may cause problems. Stateless EJB method is executed paralely, as many http requests are made. There is no synchronization inside session.Moussaka
M
4

Stateless beans: Every thread/request will get different instance of EJB from pool. SLB should not hold any user session data, any state. The same code may be executed in parallel. One instance is accessed by one thread at a time.

Statefull beans are synchronized for user session. Every user will get own session scoped instance. Second thread/request will wait until the first thread finishes. Statefull EJB can hold user specific data. One user cannot execute same code in parallel. Different users may execute same code in parallel.

If accessing a resource that does not allow parallel access use Singleton EJB. As name implies there is only one instance. By default EJB Singleton can be accessed only by one thread (Container Managed Concurrency and @Lock(WRITE)).

Mcclelland answered 12/5, 2015 at 11:17 Comment(0)
D
0

Stateless/Stateful session beans are thread safe. Because each request will get a dedicated instance of the bean and so it doesn't need to be synchronized.

Singleton session beans are shared and needs to be synchronized either by the container (Container Managed Concurrency - CMC) or by the user (Bean Managed Concurrency - BMC).

Druggist answered 21/6, 2016 at 22:23 Comment(1)
Singleton EJB are by default CMC.Moussaka
C
-3

Very True thing about EJB beans is that once you have created EJB 3.0 beans then the methods of the EJB is by default Synchronized.

e.g.

@Statelss Class EJBclass {

void someMethod(){ }

}

now if you will make this someMethod Synchronize it will show Error like it is can not be Synchronize at this level as it is synchronized.

EJB 3.0 Beans are smart and performance is good.

Cockade answered 15/4, 2013 at 10:16 Comment(0)
T
-3

Enterprise java beans are not synchronized . As session beans are maintained by ejb container so you have to implement synchronization logic in application level.

Tantivy answered 2/7, 2013 at 16:25 Comment(1)
Are you sure about this? I think the JEE standard is very clear and only Entity Beans allow configuration of current access to their methods. Please correct me if I'm wrong.Messaline

© 2022 - 2024 — McMap. All rights reserved.