title: How to get JDBC connections obtained from a JNDI datasource to participate in a UserTransaction using Weblogic 10.3?
body: <p>I am currently retrieving both a UserTransaction and a DataSource from a Weblogic 10.3 server using JNDI.</p>
  
  <p>I have set the Datasource up to 'Support Global Transactions' and to use 'Logging Last Resource'</p>
  
  <p>My hope was that by beginning a UserTranscation and then retrieving a JDBC connection from the Datasource the connection would participate in the Transaction.</p>
  
  <p>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.</p>
  
  <p>Are my above assumptions correct?</p>
  
  <p>Can anyone point me in the direction of some documentation or samples on how to achieve this?</p>
  
  <p>Many thanks in advance</p>
  
  <p>UPDATE:</p>
  
  <p>As requested here is a skeleton outline of the code I am using:</p>
  
  <pre><code>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
    }
  }
  }
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>In order to resolve this issue I had to do 2 things:</p>
  
  <ol>
  <li><p>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).</p></li>
  <li><p>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.</p></li>
  </ol>
  
  <p>I have accepted Pascal Thivent's answer below because it explained both of these issues.</p>