简体   繁体   中英

How to integrate operations of jpa repository methods i.e. save into xa resource transactions?

I want to make operations performed by crud repository of jpa as part of xa transactions ie they should be committed when xa resource is committed.

Following is the code in which save method uses xa resource and saveTeam method uses crud repository method ie save. I have tried transaction propagation supported, not_supported, etc parameters but didn't worked

public boolean save() throws Exception {
        OracleXADataSource oracleRootSource = new OracleXADataSource();
        oracleRootSource.setUser("root");
        oracleRootSource.setPassword("root");
        oracleRootSource.setURL("jdbc:oracle:thin:@localhost:1521:xe");
        XADataSource xaDS = oracleRootSource;
        XAConnection xaCon = xaDS.getXAConnection("root", "root");
        XAResource xaRes = xaCon.getXAResource();
        Connection con = xaCon.getConnection();
        Statement stmt = con.createStatement();
        Xid xid = new MyXid(100, new byte[] { 0x01 }, new byte[] { 0x02 });
        xaRes.start(xid, XAResource.TMJOIN);
//      stmt.executeUpdate("Insert into school values ( " + 202 + ", 'Second')"); 
        this.saveTeam(new Team("second"));
        xaRes.end(xid, XAResource.TMSUCCESS);
        return true;
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public Team saveTeam(Team team) {
        Team te = repo.save(team);
        return te;
    }

I want to see changes done by saveTeam method when xresource is commited. currently changes are visible in db when save method is executed in save Team method.

在application.properties文件中添加spring.jpa.show-sql=true这行

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