简体   繁体   中英

What is exactly the process of transaction manager in JTA

After reading a lot of blogs about JTA, there is still some points confuse me.

For example I found in JTA Example

The blog provide a picture to explain relationship between components of a distributed-transaction

在此处输入图像描述

This picture seems to imply Java Application can directly access to Resource Manager which in this case is JDBC Driver.

But in my opinion, the Java Application can only access to Transaction Manager and all the accesses for DataSource is controlled by Transaction Manager.

Is it something wrong with my understanding?



For another confusion.

Since we can control transaction like this:

XADataSource xaDS;
XAConnection xaCon;
XAResource xaRes;
Xid xid;
Connection con;
Statement stmt;
int ret;

xaDS = getDataSource();
xaCon = xaDS.getXAConnection("jdbc_user", "jdbc_password");
xaRes = xaCon.getXAResource();
con = xaCon.getConnection();
stmt = con.createStatement();
xid = new MyXid(100, new byte[]{0x01}, new byte[]{0x02});
try {
    xaRes.start(xid, XAResource.TMNOFLAGS);
    stmt.executeUpdate("insert into test_table values (100)");
    xaRes.end(xid, XAResource.TMSUCCESS);

    ret = xaRes.prepare(xid);
    if (ret == XAResource.XA_OK) {
        xaRes.commit(xid, false);
    }
} catch (XAException e) {
    e.printStackTrace();
} finally {
    stmt.close();
    con.close();
    xaCon.close();
}

Is Transaction Manager just a convenient way to do things that we can manually did above?

Your example has only one resource and could be executed only with JDBC without JTA.

JTA is about distributed transactions

that is, transactions that access and update data on two or more networked computer resources.

A good example should involve at least two resources:

2 different databases

or

1 database and 1 file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM