Transaction Propagation with JBoss - Case 4
paulohbmelo Sep 12, 2012 10:41 AMHi folks!
Here in my job, I'm with a case that have 2 web java applications needing to make transactional (atomic and real time) integration. Soon I thought in distributed EJB transactions. I started researching and found the following article: https://community.jboss.org/wiki/TransactionPropagationWithJBoss that, by the way it's very good but, unfortunately, the case that I need don't was exemplified. The case 4!
In the article, the author say that "the case 4 is covered at length in the JTS programmers guide" but, the samples in the guide not yet clear to me.
In my case, there's an app1 that lookup a connection to make some atomic operations and need to make some operations in app2 too but, all the operations may be in the same transaction. Soon we created an EJB session bean for the app2 and we configured the same (like change the deployment descriptor to support calls by IIOP transport).
The app2 use Spring and we used the SpringBeanAutowiringInterceptor to integrate with EJB 3 and it's running very well but, I don't know how (and where to make the "bind") to integrate the local connection in app1 with the EJB connection in the same transaction.
The server code is like this:
@Stateless
@Remote(TestService.class)
@RemoteBinding(jndiBinding="TestServiceEJB")
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class TestServiceEJB implements TestService {
@Autowired
private TestBusiness testBusiness;
@Override
public void includeTest(TestTO testTO) throws RemoteException {
try {
Test test = new Test();
test.setId(testTO.getId());
test.setDesc(testTO.getDesc());
test.setSigl(testTO.getSigl());
testBusiness.incluir(test);
} catch (Exception ex) {
ex.printStackTrace();
throw new RemoteException(ex.getMessage());
}
}
}
And the client source is like this:
...
try {
InitialContext ctxLocal = new InitialContext();
ctxRemoto = getRemoteContext(); //context to lookup EJB with IIOP support
orb = new ORBWrapper(); // like in the article
orb.start();
OTSManager.get_current().begin();
DataSource dataSource = (DataSource) ctxLocal.lookup("jdbc/test-xa");
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.executeUpdate("INSERT INTO TEST (TEST_DESC, TEST_SIGL, TEST_STAT) VALUES ('TESTE_APP1', 'TST', 'A')");
statement.close();
TestService testService = (TestService) ctxRemoto.lookup("TestServiceEJB");
TestTO testTO = new TestTO();
testTO.setDesc("TEST_APP2");
testTO.setSigl("TT");
testService.includeTest(testTO);
OTSManager.get_current().commit(true);
orb.stop();
} catch (Exception ex) { //many catch's omitted to simplify.
rollbackTransaction();
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
...
Then I do the question: where (is) put the integration/bind between the connections (app1 local and EJB).
BTW, I'm using JBoss 4.2.3 with EJB3 in app2, Spring 3.1.1 and JPA 1.0 (Hibernate 3) in app2, Java 6 in both and "pure" JDBC in app1.
Sorry for my English and for don't have put the code in quotes (where is? ou how to do?)
Thank you!