java ee transaction and datasource concepts

1. What is a transaction?
A transaction groups units of work and save the them all at once, or at any failure, roll back all the changes.

2. What are the features of a transaction?
ACID: Atomicity, Consistency, Isolation and Durability.

3. What are the major issues in app development regarding transaction?
3.1 Atomicity: if necessary, group several updates into one transaction to make sure the changes are either all saved or rolled back.
3.2 Consistency: when multiple threads accessing same record, make sure no changes/updates are lost.

4. What is the usual isolation most RDBMS databases have?
Read-Commit: a SELECT query only sees data committed before the query began.
This prevents Dirty Read(reading uncommitted data), but there may be Non-Repeatable Read (reading the same record might return different data in same transaction) and Phantom Read (ie, newly committed data becomes visible to the query that were not visible earlier in the transaction).

5. When a transaction starts?
When an application connects to a database and executes some query, a transaction starts.

6. What is the java API that represents a database transaction?
A jdbc connection represents a database transaction. Normally when you get a connection, the transaction starts; when you close the connection, the transaction ended.

7. What is the default commit mode for jdbc connection?
jdbc connection commit mode defaults to true. this means after a query completes, by default the transaction would try to commit.

8. What represents a transaction task in Java EE?
A transaction task is represented by a method that performs a database query. Typically, an EJB method is by default transaction aware and normally represents a transaction task.

9. What is local transaction?
Local transaction involves only one local database/resource manager.

10. What is global/distributed transaction?
Global/distributed transactions may involve more than one databases/resource managers. If global/distributed transactions involves more than one databases, it'll use Two-Phase commit.

11. What is jdbc transaction?
jdbc transaction by interface java.sql.Connection and can only work on one single database.

12. What is JTA (Java Transaction API)
JTA is java's transaction API for global/distributed transactions.

13. What is data source
Data source is used to manage database connections. Usually a pool of connections would be created for an application deployed to JEE containers. javax.sql.Datasource: A factory for connections to the physical data source that this DataSource object represents.

14. What is transaction management service provided by JEE containers?
Transaction management service is one of the most important services provided by most JEE compliant application servers. This service would monitor any objects/components that participate in a transaction and decides in the end if the transaction should commit or roll back. For a local JTA transaction, the transaction management service would make sure that the jdbc connection would be the same one for any objects that gets connection from configured data source.

15. What is implicit transaction management in app development?
Also called Container Managed Transactions(CMT). With the transaction management service provided by JEE containers, programming object don't need to manually start/commit a transaction manually. For instance, with the EJB technology, any ejb method implicitly start a transaction and the transaction would commit when the method completes.

16. What is explicit transaction management in app development?
Developers in their code manually start/commit a transaction. For EJBs, it must be of Bean Managed Transactions(BMT).

For jdbc connections:
connection.setAutoCommit(false);
....... // tasks to perform
connection.commit();

For JTA, it has an interface javax.transaction.UserTransaction:
// it can be injected with @Resource
@Resource UserTansaction utx;

// if in ejb, can get from ejb context
@Resource SessionContext ejbContext;
UserTransaction utx = ejbContext.getUserTransaction();

utx.begin();
...... // tasks to perform
utx.commit();

17. What is JEE transaction scope?
JEE transaction scope refers to the objects that participate in a transaction. For instance, session EJBs, entity beans etc.

18 What is JEE transaction scope propagation?
The same transaction (connection) would be passed(propagated) to all the participants of a transaction. This includes objects such as the EJBs that are called by the first EJB that starts the transaction and objects that have jdbc methods to access databases, and injected persistent context and etc. This provides Atomicity in that all the changes would be committed or rolled back.

19. What is default transaction attribute for EJBs?
default is Required: when an EjB method is called, if there's already a transaction available, this transaction would be used; otherwise, a new transaction would be started. The same transaction would be passed to any other EJBs, objects that need access to the database.

Support: if a transaction available, the ejb join the transaction.
RequiresNew: the ejb would start a new transaction. if a transaction already exists, the transaction would be suspended and resume after the ejb's transaction completes.

20 What is Optimistic Locking?
Optimistic Locking is commonly used to ensure data Consistency when there are concurrent access to same record. It assumes no other threads would update the same record. Add a version column to the table in question, morally a numeric type, like long/integer etc. Before commit update, check this column has the same value as that of last read. If they match, the update would be successful and transaction committed. If they don't match, then the record has been modified by some other thread and an OptimisticException should be raised and depends on requirement, it can re-try or just notifies client program components or end users.

Manual implementation: A typical update statement thus becomes(assume latest version=12345):
   update student set name='john', version = version + 1
   where id=120 and version=12345;

JPA implementation: use annotation @Version on the column. This property does not means to be updated by your code. JPA would manage the column automatically for you. When a non-relationship persistent field or property is changed, OR when a relationship attribute owned by the entity is modified, JPA will increment the version value.

21. What is JBoss no-tx-datasource?
When you configure a datasource like this in JBoss, you don't have JBoss' transaction management service available. You virtually said this: "I would like to manage transactions by myself". Why people want this? no idea.

22. What is JBoss local-tx-datasource?
This is the local JTA datasource configuration for JBoss and the transaction management service is turned on. This is the most commonly used datasource configuration if your application interacts with only one database.

22. What is JBoss xa-datasource?
If your application interacts with more than one databases, you need Two-Phase commit and this xa datasource configuration will do that.

23 System exception and Application exception
System exceptions represents unknown errors. System exceptions include java.lang.RuntimeException and its subclasses. EJBException is a subclass of RuntimeExcpetion and thus a system exception. When a System Exception is thrown, transaction would be rolled back.

Application exceptions are on the other hand part of your business logic and always delivered directly to client and would not cause a transaction to roll back, so that client code would have opportunity to catch the exception and recover from it based on business requirement. Application exceptions can be created by developers and normally extends java.lang.Exception.

24. What are the roll back API methods?
For Container Managed Transactions(CMT), it would catch any application exception interested and use EJBContext.setRollbackOnly() to roll back the transaction.

For Bean Managed Transactions(BMT), it would catch any application exception interested and use javax.transaction.UserTransaction.rollback().


 

猜你喜欢

转载自jxee.iteye.com/blog/1722175