开发者

UserTransaction issue in java

I have existing project which uses 2 database (DB2),and the records which are saved in the 2 databases are related.So the transactions need to be maintained.Eg whenever a new thing is to be added then entries must be done to x number of tables in Database1 and and y number of tables in database2. Now in the code that is preexisting(developed by somone else) i see some thing like

UserTransaction utx = getTranscationU();  
//getTranscation() is a user defined method as show   below

public UserTransaction getTransactionU()  {
        InitialContext ic = new InitialContext();
        return (UserTransaction) ic.lookup("java:comp/XYZ");
    }

so in code following sequence is followed:

  1. start utx
  2. create a seperate entity manager ems for db1 and db2
  3. create entries in both tables using this ems
  4. if any exception is thrown then rollback utx

Now my question is

  1. will this code make sure that entries are entered in both db or none of them?
  2. Can any one explain me wh开发者_运维知识库at does code inside getTransactionU() does?
  3. where is XYZ defined?


  1. When the UserTransaction is initialized, if both entity managers are using Datasources linked to the transaction manager, all its operations will be included at the transaction. The transaction must be a XATransaction (distributed) if you are accessing different databases.
  2. The getTransactionU() method is accesing the Java EE container's Naming Directory through JNDI, where the Transaction manager has to be configured.
  3. It's the path of the transaction manager set up on your server.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜