I am currently retrieving both a UserTransaction and a DataSource from a Weblogic 10.3 server using JNDI.
I have set the Datasource up to 'Support Global Transactions' and to use 'Logging Last Resource'
My hope was that by beginning a UserTranscation and then retrieving a JDBC connection from the Datasource the connection would participate in the Transaction.
This does not appear to be the case, and my insert statements are being commited straight away and rolling back the transaction does not have any effect.
Are my above assumptions correct?
Can anyone point me in the direction of some documentation or samples on how to achieve this?
Many thanks in advance
UPDATE:
As requested here is a skeleton outline of the code I am using:
private void doSomething() {
Connection conn = null;
try {
Hashtable env = new java.util.Hashtable();
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(javax.naming.Context.PROVIDER_URL,"t3://localhost:8080");
InitialContext ctx = InitialContext(env));
UserTransaction transaction = null;
transaction = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
DataSource dataSource = (DataSource) context.lookup("jdbc/xxxxx/DataSource");
conn = dataSource.getConnection();
transaction.begin();
// JDBC code goes here
transaction.commit();
} catch(Exception e) {
// TODO
if (transaction != null) {
try {
transaction.rollback();
} catch (Exception ex) {
// TODO
}
} finally {
if (con != null) {
conn.close
}
}
}
UPDATE 2:
In order to resolve this issue I had to do 2 things:
Change the order of the code to firstly begin the user transaction and then get a connection from the Datastore (as pointed out by Pascal Thivent).
Change the Datasource referenced by '"jdbc/xxxxx/DataSource"' to an XADatasource. This is because I was calling code inside the user transaction that used another Datasource that was already configured to support LLR and as pointed out by Pascal Thivent below you can only have one LLR Datasource participate in a transcation.
I have accepted Pascal Thivent's answer below because it explained both of these issues.