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:
- start utx
- create a seperate entity manager ems for db1 and db2
- create entries in both tables using this ems
- if any exception is thrown then rollback utx
Now my question is
- will this code make sure that entries are entered in both db or none of them?
- Can any one explain me wh开发者_运维知识库at does code inside getTransactionU() does?
- where is XYZ defined?
- 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.
- The getTransactionU() method is accesing the Java EE container's Naming Directory through JNDI, where the Transaction manager has to be configured.
- It's the path of the transaction manager set up on your server.
精彩评论