Implement external transaction coordination
You can implement external transaction coordination by using the procedure that is listed here.
About this task
To implement external transaction coordination:
Procedure
- Create the custom API that you want to implement your external
transaction process, but DO NOT commit the external transactions.
Instead, register the external transaction object with the YFSEnvironment
by calling the (YFSEnvironment)env.setTxnObject(String ID, Object
txnObj).
The String ID is a unique name that is used by the user exit implementation class to identify the custom transaction object.
The following example illustrates a simple custom API.
public class doSomethingAPI { private YFCLogCategory cat = YFCLogCategory.instance("DoSomethingAPI"); public doSomethingAPI() // constructor is empty { } public void writeToDB (String key, YFSEnvironment oEnv) { try { Driver aDriver = (Driver)Class.forName("db2.jdbc.DB2Driver").newInstance(); String url = "jdbc:db2:thin:@127.0.0.1:1521:qotree2"; Connection conn = DriverManager.getConnection(url, "Scott", "Tiger"); conn.setAutoCommit(false); String sql = "insert into TxnTest (key) values ('" + key + "')"; Statement stmt = conn.createStatement(); stmt.executeUpdate(sql); oEnv.setTxnObject("YDBconn", conn); } catch (Exception e) { System.out.println ("Caught Exception :\n" + e); } } public Document doSomething(YFSEnvironment env, Document doc) throws Exception { System.out.println("Executing doSomething method..........."); writeToDB ("doSomething", env); return doc; } }
- Implement the com.yantra.yfs.japi.ue.YFSTTxnCoordinatorUE
user exit interface to commit the external transactions either before
or after Sterling™ Order Management System perform
its commits. Then implement the rollback method so that if the Sterling Order Management System transaction
triggers a rollback, the afterYantraTxnRollback(YFSEnvironment oEnv)
method is called to rollback the external transactions as well. Implement
the following methods to accomplish this:
- beforeYantraTxnCommit(YFSEnvironment oEnv)
- afterYantraTxnCommit(YFSEnvironment oEnv)
- afterYantraTxnRollback(YFSEnvironment oEnv)
Note: You can use either the beforeYantraTxnCommit or the afterYantraTxnCommit user exit to synchronize commits, depending on your integration requirements.Calling(YFSEnvironment)env.getTxnObject(ID)enables these methods to obtain the handle to the external transaction object that was previously registered by the (YFSEnvironment)env.setTxnObject(String ID, Object txnObj). Note the ID is the same in both the getTxnObject call and the setTxnObject call.
The following is an example of the YFSTTxnCoordinatorUE user exit interface implementation.
public class afterTxnCommit implements YFSTxnCoordinatorUE { public void beforeYantraTxnCommit (YFSEnvironment oEnv) throws YFSUserExitException { // before method is not implemented because after method is implemented. } public void afterYantraTxnCommit (YFSEnvironment oEnv) throws YFSUserExitException { System.out.println ("Entering method afterYantraTxnCommit........."); try { Connection ydbConn = (Connection)oEnv.getTxnObject("YDBconn"); ydbConn.commit(); } catch (Exception e) { System.out.println ("Caught Exception :\n" + e); } } public void afterYantraTxnRollback (YFSEnvironment oEnv) throws YFSUserExitException { System.out.println ("Entering method afterYantraTxnRollback........."); try { Connection ydbConn = (Connection)oEnv.getTxnObject("YDBconn"); ydbConn.rollback(); } catch (Exception e) { System.out.println ("Caught Exception :\n" + e); } }
- Launch the Applications Manager and navigate to System Administration > User Exit Management to configure the YFSTxnCoordinatorUE Implementation
class.
For more information about the YFSTxnCoordinatorUE user exit interface definition, see the Javadoc information.