We have two EJB session beans as given below;
#Stateless
public class MyStatelessSessionBean{
#EJB
MyStatefulSessionBean statefulBean;
public void methodA(){
statefulBea.methodB();
}
}
#Stateful
# TransactionAttribute(TransactionAttributeType.REQUIRED)
public class MyStatefulSessionBean {
#Asynchronous
public void methodB(){
}
}
A client, which is not in any in any transaction, invoke methodA of MyStatelessSessionBean. How many distict transactions will be started by container after all processing has completed ?
There will be 2 transactions started. As EJB 3.1 specification states in point 4.5.3:
Client transaction context does not propagate with an asynchronous method invocation. From the Bean Developer’s view, there is never a transaction context flowing in from the client. This means, for example, that the semantics of the REQUIRED transaction attribute on an asynchronous method are exactly the same as REQUIRES_NEW.
Related
I am trying to understand the transactions management and try to use its power in my already existing application developed in Struts 2, EJB 3 and hibernate 5.2.
Now I have ejb in my business layer like below
#Stateless
#TransactionManagement(TransactionManagementType.CONTAINER)
public class MyEJb implements ejbxyz {
#Override
public void method(){
Dao dao=new Dao() //Dao class is simple java class
dao.fooMethod(); //this method updates some record
dao.barMethod(); // this method updates some other record
}
}
public class Dao{
fooMethid(){
Session session=sessFactory.openSession();
session.beginTransaction();
session.update(x);
}
barMethod(){
try{
Session session=sessFactory.getCurrentSession();
session.getNamedQuery("xyz").executeUpdate();
}catch(HibernateException ex){
session.getTransaction.rollback();
}
}
}
I understand that Transaction management should be done at service layer(at ejb in my case). But how can I achieve this over there. ?
Now the dependency is if barMethod() fails to update the record then I need to rollback the changes made in fooMethod. So basically I need both the methods to be done in one transaction.
When I execute the application it throws the below exception
Exception while barMethod getNamedQuery is not valid without active transaction
Its because I am not beginning any transaction in barMethod. But then I really dont want to start a new transaction and want to continue with the older transaction started in fooMethod.
Container managed transactions are indeed suported out of the box for EJB beans. However, your Dao class is not a managed bean - it is a regular pojo that you instantiate manualy - therefore it does not participate in any transaction started by your other ejb.
So move your Dao to separate file, annotate it with #Stateless and then inject it into your service using #EJB private Dao dao;
There is more to transactions in Ejb container though. You can control the transaction support on method level via #TransactionAttribute annotation, that specifies how should the container invoke your method with regard to transaction. That way you can control, whether your method requires its own transaction, or if it shall participate in a transaction initiated by the caller(e.g. when invoked from ejb bean). For more info have a look at official Java EE tutorial
I have a problem accessing stateless EJBs in the preDestroy method of an singleton. I need to log the shutdown of the application server in an database table.
This is what the singleton looks like:
#Startup
#Singleton
public class ServerShutdown {
#EJB
TableFacade tableFacade;
#PreDestroy
private void shutdown() {
TestEntity e = tableFacade.find("test");
//do something
}
}
Here's example code of the stateless bean:
#Stateless
public class TableFacade {
...
public TestEntity find(String test) {
Query query =
getEntityManager().createNamedQuery("TestEntity.namedQuery");
return (TestEntity) query.getSingleResult();
}
}
If the server is shutting down, the preDestroy method is accessed and the EJB method is called. But during the call, the server seems to force the shutdown process and cancels the calling of the EJB method.
I'm using Java EE 6, JDK 1.8, EJB 3.1, eclipselink 2.5.2.
Thanks in advance
The #predestroy should only do ejb resource cleanup, such as connection, variable etc...
Your problem has to do with the transaction context, infact from the spec:
The PreDestroy lifecycle callback interceptor methods for stateless
and stateful session beans execute in an unspecified transaction
context.
And then:
For example, it would be wrong to perform database operations within a
stateful session bean’s PostConstruct or PreDestroy lifecycle callback
interceptor methods and to assume that the operations are part of the
client’s transaction. The PostConstruct and PreDestroy methods for
stateful and stateless session beans are not controlled by a
transaction attribute because handling rollbacks in these methods
would greatly complicate the session instance’s state diagram.
So, it is not explicitly forbidden, but you are warned that things may go wrong.
According to the ejb 3.2 spec, singleton beans are allowed to access ejb's in their pre destroy method. See section 4.8.6 Table 3. If a singleton bean needs to access another singleton bean, then it must declare its dependency using the #DependsOn annotation. The example provided by the original poster should work.
In the following setup, does method B run in a (new) transaction?
An EJB, having two methods, method A and method B
public class MyEJB implements SessionBean
public void methodA() {
doImportantStuff();
methodB();
doMoreImportantStuff();
}
public void methodB() {
doDatabaseThing();
}
}
The EJB is container managed, with methodB in requires_new transaction, and method A in required transaction. thus:
<container-transaction id="MethodTransaction_1178709616940">
<method id="MethodElement_1178709616955">
<ejb-name>MyName</ejb-name>
<method-name>*</method-name>
<trans-attribute>Required</trans-attribute>
</method>
<method id="MethodElement_1178709616971">
<ejb-name>MyName</ejb-name>
<method-name>methodB</method-name>
</method>
<trans-attribute>RequiresNew</trans-attribute>
</container-transaction>
Now let another EJB call methodA with an EJB method call.
methodA now runs in an transaction. Will the subsequent call to methodB from methodA run in the same transaction, or does it run in a new transaction?
(mind, it's the actual code here. There is no explicit ejb-call to method B)
Your call to methodB() is an ordinary call of a method, not intercepted by the EJB container; at run-time the EJB container will inject a proxy and not an instance of your class, this is the way it intercepts calls and setup the environment before calling your method. If you use this you're calling a method directly and not through the proxy. Hence both methods will use the same transaction, regardless to what is defined in ejb-jar.xml for calls through EJB interfaces.
inject SessionContext, and ask it for your proxy instance:
#Stateless
public class UserFacade implements UserFacadeLocal {
#Resource
private SessionContext context;
#Override
#TransactionAttribute(TransactionAttributeType.REQUIRED)
private void create(User user) {
System.out.println("Users Count: "+count()); //invocation#1
System.out.println("Users Count Through Context: "+context.getBusinessObject(UserFacadeLocal.class).count()); //invocation#2
}
#Override
#TransactionAttribute(TransactionAttributeType.NEVER)
public int count() {
return ((Long) q.getSingleResult()).intValue();
}
}
in 'invocation#1' this is a Local call, not passing through proxy, it will return the count
in 'invocation#2' this is a call through the proxy, and hence you annotate it not to support transaction -which is now opened by create(user) method-, this invocation will throw a transaction exception:
javax.ejb.EJBException: EJB cannot be invoked in global transaction
They will use the same transaction.
If I remember well, the transaction is started by the container "before" the method is invoked and commited after it "finish".
Since "a" calls "b", "b" would use the same transaction.
:S
I guess the best thing you can do is test it to verify it! :)
If i have two methods inside an EJB bean, one with Transaction attribute of NOT_SUPPORTED that needs to call the other with REQUIRED, can i expect the transaction to kick in if i make the call through an injected bean:
#Stateless
#LocalBean
public class LeBean {
#EJB LeBean bean;
#TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void someMethod(){
...
bean.otherMethod();
}
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public void otherMethd(){
...
}
}
or can i make the call locally like so:
#Stateless
#LocalBean
public class LeBean {
#TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void someMethod(){
...
otherMethod();
}
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public void otherMethd(){
...
}
}
right now someMethod() takes a long time to process information before reaching otherMethod() and so the transaction times out, even though i have stated NOT_SUPPORTED as the transactionAttribute for the first method.
can i expect the transaction to kick in if i make the call through an
injected bean:
You HAVE to make the call through the injected bean if you want a transaction. The calls have to be made through the business interface, or else your transaction attribute will be ignored. In your case if you call otherMethod() from a method that has no transaction, or a suspended transaction (i.e. - NOT_SUPPORTED) then it is simply a POJO call.
Technically speaking otherMethod() will "ride on top of" the transaction of someMethod() if one did exist. For example you have NOT_SUPPORTED for someMethod(), but if it were REQUIRED or REQUIRES_NEW, then otherMethod() would share in that transaction. A rollback for someMethod() would also rollback operations from otherMethod().
right now someMethod() takes a long time to process information before
reaching otherMethod() and so the transaction times out
That is a different issue altogether. You may wish to increase your transaction timeout, and consider running this as a separate asynchronous process.
Let's I have Stateless bean with CMT. I have 3 methods in bean, 2 with TransactionAttributeType.REQUIRED. And both method are called from third method. How can I check when transaction is active? i want check
#Stateless
#TransactionManagement(TransactionManagementType.CONTAINER)
public class MyBean
{
public RetType methodA()
{
methodB();
//.... is CMT active there?
methodC();
}
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public RetType methodB(){}
#TransactionAttribute(TransactionAttributeType.REQUIRED)
public RetType methodC(){}
}
The TransactionAttributeType.REQUIRED attribute is the default for container managed transactions bean methods, so even if you didn't annotate it, methodA runs in a transaction that starts as soon as the method starts (unless you call the method from another active transaction, in that case the method simply join the current transaction).
The transaction ends when the method upon method exit (again unless called from another transaction). Any method called by methodA, unless annotated with TransactionAttributeType.REQUIRES_NEW, joins the current transaction.