Search in sources :

Example 11 with XAConnection

use of javax.sql.XAConnection in project druid by alibaba.

the class DruidXADataSource method getXAConnection.

@Override
public XAConnection getXAConnection() throws SQLException {
    DruidPooledConnection conn = this.getConnection();
    Connection physicalConn = conn.unwrap(Connection.class);
    XAConnection rawXAConnection = createPhysicalXAConnection(physicalConn);
    return new DruidPooledXAConnection(conn, rawXAConnection);
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) XAConnection(javax.sql.XAConnection)

Example 12 with XAConnection

use of javax.sql.XAConnection in project spring-boot by spring-projects.

the class PoolingDataSourceBeanTests method setDataSource.

@Test
public void setDataSource() throws Exception {
    XADataSource dataSource = mock(XADataSource.class);
    XAConnection xaConnection = mock(XAConnection.class);
    Connection connection = mock(Connection.class);
    given(dataSource.getXAConnection()).willReturn(xaConnection);
    given(xaConnection.getConnection()).willReturn(connection);
    this.bean.setDataSource(dataSource);
    this.bean.setBeanName("beanName");
    this.bean.afterPropertiesSet();
    this.bean.init();
    this.bean.createPooledConnection(dataSource, this.bean);
    verify(dataSource).getXAConnection();
    TransactionManagerServices.getTaskScheduler().shutdown();
}
Also used : XADataSource(javax.sql.XADataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 13 with XAConnection

use of javax.sql.XAConnection in project spring-boot by spring-projects.

the class NarayanaDataSourceBeanTests method shouldGetConnectionAndCommit.

@Test
public void shouldGetConnectionAndCommit() throws SQLException {
    Connection mockConnection = mock(Connection.class);
    XAConnection mockXaConnection = mock(XAConnection.class);
    given(mockXaConnection.getConnection()).willReturn(mockConnection);
    given(this.dataSource.getXAConnection("", "")).willReturn(mockXaConnection);
    Properties properties = new Properties();
    properties.put(TransactionalDriver.XADataSource, this.dataSource);
    Connection connection = this.dataSourceBean.getConnection();
    assertThat(connection).isInstanceOf(ConnectionImple.class);
    connection.commit();
    verify(this.dataSource, times(1)).getXAConnection("", "");
    verify(mockXaConnection, times(1)).getConnection();
    verify(mockConnection, times(1)).commit();
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) Properties(java.util.Properties) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 14 with XAConnection

use of javax.sql.XAConnection in project aries by apache.

the class TxDBServlet method insertIntoTransaction.

/**
	 * This method demonstrates how to enlist JDBC connection into Transaction according OSGi enterprise specification.
	 * 
	 * @param xads XADataSource
	 * @param tm TransactionManager
	 * @param value which will be inserted into table
	 * @param toCommit Specify if the transaction will be committed or rolledback
	 * @throws SQLException
	 * @throws GenericJTAException
	 */
private void insertIntoTransaction(XADataSource xads, TransactionManager tm, String value, boolean toCommit) throws SQLException, GenericJTAException {
    XAConnection xaConnection = xads.getXAConnection();
    Connection connection = xaConnection.getConnection();
    XAResource xaResource = xaConnection.getXAResource();
    try {
        tm.begin();
        Transaction transaction = tm.getTransaction();
        transaction.enlistResource(xaResource);
        PreparedStatement insertStatement = connection.prepareStatement(INSERT_INTO_TABLE);
        insertStatement.setString(1, value);
        insertStatement.executeUpdate();
        if (toCommit) {
            transaction.commit();
        } else {
            transaction.rollback();
        }
    } catch (RollbackException e) {
        throw new GenericJTAException(e);
    } catch (SecurityException e) {
        throw new GenericJTAException(e);
    } catch (IllegalStateException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicMixedException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicRollbackException e) {
        throw new GenericJTAException(e);
    } catch (SystemException e) {
        throw new GenericJTAException(e);
    } catch (NotSupportedException e) {
        throw new GenericJTAException(e);
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) XAResource(javax.transaction.xa.XAResource) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) XAConnection(javax.sql.XAConnection)

Example 15 with XAConnection

use of javax.sql.XAConnection in project aries by apache.

the class TxDBServlet method printTable.

// end of doPost(HttpServletRequest request, HttpServletResponse response)
/**
	 * Prints the table 
	 * @param xaDataSource
	 * @throws SQLException
	 */
private void printTable(XADataSource xaDataSource) throws SQLException {
    XAConnection xaConnection = xaDataSource.getXAConnection();
    Connection connection = xaConnection.getConnection();
    PreparedStatement selectStatement = connection.prepareStatement(SELECT_TABLE);
    ResultSet set = selectStatement.executeQuery();
    pw.write("<center><br><table>");
    pw.write("<tr BGCOLOR=#FFCC33>");
    pw.write("<td><font face=\"Tahoma\"><b>VALUES</font></td>");
    pw.write("</tr>");
    while (set.next()) {
        pw.write("<tr>");
        pw.write("<td><font face=\"Tahoma\">");
        pw.write(set.getString("value"));
        pw.write("</font></td>");
        pw.write("</tr>");
    }
    pw.write("</table><br></center>");
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) XAConnection(javax.sql.XAConnection)

Aggregations

XAConnection (javax.sql.XAConnection)25 Connection (java.sql.Connection)19 Test (org.junit.Test)14 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 XAResource (javax.transaction.xa.XAResource)4 PreparedStatement (java.sql.PreparedStatement)3 SystemException (javax.transaction.SystemException)3 Transaction (javax.transaction.Transaction)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Properties (java.util.Properties)2 RollbackException (javax.transaction.RollbackException)2 XAConnectionWrapper (org.apache.aries.tx.control.jdbc.xa.connection.impl.XAConnectionWrapper)2 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1