I have a trivial test case that uses a single session factory with two jndi data source connections.
Code:
...
public void testReasoningDatasource() throws Exception {
JNDIDataSourceHelper.init(JNDI_UNIT_TEST_HELPER_PROPERTIES);
InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup(JNDIDataSourceHelper
.getJndiName2());
Connection connection = dataSource.getConnection();
Session session = getSessionFactory().openSession(connection);
Case aCase = (Case) session.load(Case.class,
CASE_ID);
String title = (String) aCase.getTitles().get("en");
connection.close();
assertTrue(title.equals("TEST_CASE_1"));
}
public void testJcdtEmptyDatasource() throws Exception {
JNDIDataSourceHelper.init(JNDI_UNIT_TEST_HELPER_PROPERTIES);
InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup(JNDIDataSourceHelper
.getJndiName1());
Connection connection = dataSource.getConnection();
Session session = getSessionFactory().openSession(connection);
Case aCase = (Case) session.load(Case.class,
CASE_ID);
String title = (String) aCase.getTitles().get("en");
connection.close();
assertTrue(title.equals("TEST_CASE_2"));
}
...
The only issues I see with this approach thus far are:
1) Cannot use with 2nd level cache, which is acceptable in highly transactional applications in the first place (however, I think cache regions might assist here(?))
2) A single transaction working with multiple datasources might introduced unexpected results. Particularly if a you are accessing the same entity from N datasources with the same ID. This is because of the transactional 1st level cache, and rightfully so. I don't have any use cases at present where this would be a problem. At any rate, I have test cases for this as well.
Any thoughts?
Roll