Does this run in transaction?
Let's say I have two EJBs A and B:
public class A implements AInterface {
private B b;
...
//This method will NOT access database directly
public void a() {
//do something
b.b();
//do something
}
...
}
public class B implements BInterface {
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public void b() {
//Read database
}
}
- A.a() does not connect to database, but calls B.b()
- B.b() makes a SELECT to database
- A.a() has the default transaction attribute, which in this container is REQ开发者_Python百科UIRED
Will the call to A.a() run in a transaction? Is the transaction initiated when A.a() is entered, when B.b() is entered, when the database is accessed, or some other time?
Baseline is that I don't want this to be run in an transaction, so I could use NOTSUPPORTED for A.a(), I guess (?), but I'm trying to also understand how involving or not involving database in different points of call stack affect the transactions.
The transaction doesn't depend on a database connection itself. In EJB, a transaction isn't just a database transaction. A transaction can include several resources (see XA transactions) and several method calls. Only if all of them succeed, the transaction will succeed.
In your case, A has no TransactionAttribute and runs as REQUIRED. This will open a new transaction right at the start from A.a(). The call of B.b() runs inside the transaction from A. But the database transaction isn't commited after B.b() is finished. First, A.a() has to finish. If there is for example an exception in A.a() after calling B.b(), B.b() will be rolled back.
If you want B.b() to commit in any case, I would suggest to use RequiresNew instead of NotSupported.
You'll find a description of all transaction attributes at the Java EE Tutorial.
精彩评论