JTA and JTS | Java Transaction Design strategies

To effectively manage transaction in enterprise Java applications developers do not necessarily need to the behind-the-scenes details of the Java Transaction Service(JTS) specification.

 

Regardless of the framework you are using,most enterprise java applications leverage the Java Transaction API(JTA) for transaction management.The JTA is the interface developers use to manage transactions. The Java Transaction Service(JTS),on the other hand,is an underlying transaction service that implements JTA and is used by most commercial and opens source application servers(note that there are other transaction services on the market besides JTS that can be used). Think of the relationship between JTA and JTS as similar to the relationship between JDBC and the corresponding underlying database driver;JTA is to JDBC as JTS is to the database driver. The JTA can be implemented through commercial application servers or through open source transaction managers such as JBoss Transaction Service or JTOM.

 

UserTransaction Interface

The UserTransaction interface is only  used in the Programmatic Transaction Model,primarily within EJB. The only methods in this interface the developer needs to be concerned about are the following :

  1. begin()
  2. commit()
  3. rollback()
  4. getStatus()

javax.transaction.UserTransaction.begin()

javax.transaction.UserTransaction.commit()

javax.transaction.UserTransaction.rollback()

javax.transaction.UserTransaction.getStatus()

 

TransactionManager Interface

The javax.transaction.TransactionManager interface is primarily used within the Declarative Transaction Model.For programmatic transactions you can do essentially everything with the TransactionManager interface as you can with the User-Transaction interface.However,with most methods it is better to use the UserTransaction interface and leave the TransactionManager interface alone unless you need to suspend or resume a transaction.

  1. javax.transaction.TransactionManager.suspend()
  2. javax.transaction.TransactionManager.resume()
  3. javax.ejb.EJBContext.setRollbackOnly()

Status Interface

As we saw in the previous section, we can obtain the status of the transaction through the javax.transaction.Status interface as a result of the value obtained from the UserrTransaction.getStatus() method.I am devoting a section to the transaction status because first it is sort of cool and second because it provides us with a lot of useful information regarding the state of the current transaction. The following values are contained in the Status interface:

  1. STATUS_ACTIVE
  2. STATUS_COMMITTED
  3. STATUS_COMMITTING
  4. STATUS_MARKED_ROLLBACK
  5. STATUS_NO_TRANSACTION
  6. STATUS_PREPARED
  7. STATUS_ROLLBACK
  8. STATUS_ROLLING_BACK
  9. STATUS_UNKNOW

Out of all the status values listed above,the only ones that are really useful to developers in most mainstream Java business applications are STATUS_ACTIVE,STATUS_MARKED_ROLLBACK and STATUS_NO_TRANSACTION. The following section describe these status values in more detail and how they might be userful.

 

 

 

 

posted @ 2015-08-08 17:28  xder  阅读(227)  评论(0)    收藏  举报