Java Code Examples for javax.sql.XADataSource#getXAConnection()

The following examples show how to use javax.sql.XADataSource#getXAConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * check whether commit without statement will flow by checking its transaction id
 * on client. This test is run only for client where commits without an
 * active transactions will not flow to the server.
 * DERBY-4653
 * 
 * @throws SQLException
 **/
public void testConnectionFlowCommit()
        throws SQLException {
    ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();

    PooledConnection pc = ds.getPooledConnection();
    Connection conn = pc.getConnection();

    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for XADataSource
    XADataSource xs = J2EEDataSource.getXADataSource();
    XAConnection xc = xs.getXAConnection();
    conn = xc.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();
    
    //Test for DataSource
    DataSource jds = JDBCDataSource.getDataSource();
    conn = jds.getConnection();
    testConnectionFlowCommitWork(conn, 1);
    conn.close();       
}
 
Example 2
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void testSetSchemaInXAConnection() throws SQLException {
    // tests that set schema works correctly in an XA connection.

    XADataSource dsx = J2EEDataSource.getXADataSource();
    XAConnection xac3 = dsx.getXAConnection();
    Connection conn3 = xac3.getConnection();
    Statement st3 = conn3.createStatement();
    st3.execute("SET SCHEMA SCHEMA_Patricio");
    st3.close();

    PreparedStatement ps3 = 
        conn3.prepareStatement("INSERT INTO Patricio VALUES (?, ?)");
    ps3.setString(1, "Patricio");
    ps3.setInt(2, 3);
    ps3.executeUpdate();

    assertEquals(1, ps3.getUpdateCount());
    ps3.close();
    conn3.close();
    xac3.close();
}
 
Example 3
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private static void dsConnectionRequests(
    String[] expectedValues, XADataSource ds) {
    try {
        ds.getXAConnection();
        if (!expectedValues[0].equals("OK"))
            fail (" expected connection to fail, but was OK");
    } catch (SQLException sqle) {
        assertSQLState(expectedValues[0], sqle);
    }

    dsConnectionRequest(expectedValues[1], ds, null, null);
    dsConnectionRequest(expectedValues[2], ds, "fred", null);
    dsConnectionRequest(expectedValues[3], ds, "fred", "wilma");
    dsConnectionRequest(expectedValues[4], ds, null, "wilma");
    dsConnectionRequest(
        expectedValues[5], ds, null, "databaseName=" + dbName);
    dsConnectionRequest(
        expectedValues[6], ds, "fred", "databaseName=" + dbName);
    dsConnectionRequest(expectedValues[7], 
        ds, "fred", "databaseName=" + dbName + ";password=wilma");
    dsConnectionRequest(expectedValues[8], 
        ds, "fred", "databaseName=" + dbName + ";password=betty");
}
 
Example 4
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void assertConnectionFail(String dbName) throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    // Reset to no user/password though client requires
    // a valid name, so reset to the default
    if (usingDerbyNetClient())
        JDBCDataSource.setBeanProperty(xads, "user", "APP");
    else
        JDBCDataSource.clearStringBeanProperty(xads, "user");
    JDBCDataSource.clearStringBeanProperty(xads, "password");
    JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
    try {
        xads.getXAConnection();
        fail("expected connection to fail");
    } catch (SQLException e) {
        assertSQLState("08004", e);
    }
}
 
Example 5
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertSystemShutdownFail(
    String expectedError, String dbName, String user, String password)
throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(
            xads, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(xads, "user", user);
    JDBCDataSource.setBeanProperty(xads, "password", password);
    try {
        xads.getXAConnection();
        fail("expected shutdown to fail");
    } catch (SQLException e) {
        assertSQLState(expectedError, e);
    }
}
 
Example 6
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertSystemShutdownOK(
    String dbName, String user, String password)
throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(
            xads, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(xads, "user", user);
    JDBCDataSource.setBeanProperty(xads, "password", password);
    try {
        xads.getXAConnection();
        fail("expected system shutdown resulting in XJ015 error");
    } catch (SQLException e) {
        // expect XJ015, system shutdown, on successful shutdown
        assertSQLState("XJ015", e);
    }
}
 
Example 7
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test that connections retrieved from {@code XADataSource} that are part
 * of a global XA transaction, behave as expected when {@code close()} is
 * called and the transaction is active.
 */
public void testCloseActiveConnection_XA_global()
    throws SQLException, XAException
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xa = ds.getXAConnection();
    XAResource xar = xa.getXAResource();
    Xid xid = new cdsXid(1, (byte) 2, (byte) 3);
    xar.start(xid, XAResource.TMNOFLAGS);
    // auto-commit is always off in XA transactions, so we expect
    // getAutoCommit() to return false without having set it explicitly
    testCloseActiveConnection(xa.getConnection(), false, true);
    Connection c = xa.getConnection();
    c.setAutoCommit(false);
    testCloseActiveConnection(c, false, true);
    xar.end(xid, XAResource.TMSUCCESS);
}
 
Example 8
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void assertShutdownWOUPOK(
    String dbName, String user, String password)
throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(
            xads, "shutdownDatabase", "shutdown");
    JDBCDataSource.setBeanProperty(xads, "user", user);
    JDBCDataSource.setBeanProperty(xads, "password", password);
    try {
        xads.getXAConnection();
        fail ("expected a failed shutdown connection");
    } catch (SQLException e) {
        // expect 08006 on successful shutdown
        assertSQLState("08006", e);
    }
}
 
Example 9
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Test that connections retrieved from {@code XADataSource} that are not
 * part of a global XA transaction, behave as expected when {@code close()}
 * is called and the transaction is active.
 */
public void testCloseActiveConnection_XA_local() throws SQLException {
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xa = ds.getXAConnection();
    testCloseActiveConnection(xa.getConnection(), true, false);
    Connection c = xa.getConnection();
    c.setAutoCommit(false);
    testCloseActiveConnection(c, false, false);
}
 
Example 10
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void timeoutTestDerby1144XADS() throws SQLException {
   
    XADataSource xds = J2EEDataSource.getXADataSource();
    // Test xa connection isolation
    XAConnection xpc1 = xds.getXAConnection();        
    assertPooledConnIso("XAConnection", xpc1);                 
    xpc1.close();
}
 
Example 11
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void assertShutdownUsingConnAttrsOK(
    String dbName, String user, String password) throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(
        xads, "connectionAttributes", "shutdown=true");
    try {
        xads.getXAConnection(user, password);
        fail("expected shutdown to fail");
    } catch (SQLException e) {
        // expect 08006 on successful shutdown
        assertSQLState("08006", e);
    }
}
 
Example 12
Source File: XADSAuthenticationTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void assertShutdownUsingSetShutdownOK(
        String dbName, String user, String password) throws SQLException {
    XADataSource xads = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
    JDBCDataSource.setBeanProperty(
        xads, "shutdownDatabase", "shutdown");
    try {
        xads.getXAConnection(user, password);
        fail ("expected a failed shutdown connection");
    } catch (SQLException e) {
        // expect 08006 on successful shutdown
        assertSQLState("08006", e);
    }
}
 
Example 13
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testClosedXADSConnection() throws SQLException, Exception {
    // verify that outstanding updates from a closed connection, obtained
    // from an XADataSource, are not committed, but rolled back.
    XADataSource dsx = J2EEDataSource.getXADataSource();
    XAConnection xac = dsx.getXAConnection();
    Connection c1 = xac.getConnection();
    Statement s = c1.createStatement();

    c1.setAutoCommit(false);

    // this update should be rolled back
    s.executeUpdate("insert into intTable values(2)");
    
    c1 = xac.getConnection();

    ResultSet rs = c1.createStatement().executeQuery(
       "select count(*) from intTable");
    rs.next();

    assertEquals(0, rs.getInt(1));

    rs.close();
    c1.close();
    xac.close();
    xac = null;

    PoolReset("XADataSource", dsx.getXAConnection());
}
 
Example 14
Source File: DataSourcePropertiesTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the <code>attributesAsPassword</code> property of an
 * <code>XADataSource</code> causes an explicitly specified password to be
 * sent as a property string.
 */
public void embeddedTestAttributesAsPasswordWithPassword_xa()
    throws Exception
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
    try {
        XAConnection xa = ds.getXAConnection("username", "mypassword");
        fail("Expected getXAConnection to fail.");
    } catch (SQLException e) {
        // expect error because of malformed url
        assertSQLState("XJ028", e);
    }
}
 
Example 15
Source File: DataSourcePropertiesTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the default password is not sent as an attribute string when
 * <code>attributesAsPassword</code> is <code>true</code>. The test is run
 * with an <code>XADataSource</code>.
 */
public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
    throws Exception
{
    XADataSource ds = J2EEDataSource.getXADataSource();
    JDBCDataSource.setBeanProperty(ds, "password",  "mypassword");
    JDBCDataSource.setBeanProperty(ds, "attributesAsPassword", Boolean.TRUE);
    XAConnection xa = ds.getXAConnection();
    Connection c = xa.getConnection();
    c.close();
}
 
Example 16
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testReadOnlyToWritableTran() throws SQLException, Exception
{
    // This fixture will run twice, once with embedded, once with client,
    // and insert 2 rows in addition to the 5 rows inserted during setup. 
    // The fixture tests a commit, so before running, try to remove row 
    // 6 and 7 in case this is the second run of the fixture.
    Statement s = createStatement();
    s.executeUpdate("delete from autocommitxastart where i = 6");
    s.executeUpdate("delete from autocommitxastart where i = 7");
    
    // TESTING READ_ONLY TRANSACTION FOLLOWED BY WRITABLE TRANSACTION
    // Test following sequence of steps
    // 1)start a read-only global transaction 
    // 2)finish that read-only transaction
    // 3)start another global transaction 

    XADataSource dsx = J2EEDataSource.getXADataSource();
    XAConnection xac5 = dsx.getXAConnection();
    Xid xid5a = new cdsXid(5, (byte) 119, (byte) 129);
    Connection conn5 = xac5.getConnection();
    Statement sru5a = conn5.createStatement();
    XAResource xar = xac5.getXAResource();
    xar.start(xid5a, XAResource.TMNOFLAGS);
    conn5.setReadOnly(true);

    // Read-Only XA transaction;
    // holdability: (hold, or close cursors over commit) , 
    // transaction isolation: read-committed, 
    // auto-commit false, read-only true (with embedded)
    if (usingEmbedded()) 
    {
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT, 
            Connection.TRANSACTION_READ_COMMITTED,
            false, true, conn5);
    }
    // Note: the original test had no comments about this difference
    //       between Embedded and DerbyNetClient, this has apparently
    //       been accepted behavior.
    else if (usingDerbyNetClient())
    {
        assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT, 
            Connection.TRANSACTION_READ_COMMITTED,
            false, false, conn5);
    }
    
    ResultSet rs5 = sru5a.executeQuery(
        "select count(*) from autocommitxastart");
    rs5.next();
    assertEquals(5, rs5.getInt(1));
    rs5.close();
    xar.end(xid5a, XAResource.TMSUCCESS);
    xar.commit(xid5a, true);
    conn5.close();
    
    //now start a new transaction
    conn5 = xac5.getConnection();
    sru5a = conn5.createStatement();
    xar.start(xid5a, XAResource.TMNOFLAGS);
    
    // Writeable XA transaction
    // holdability: (hold, or close cursors over commit) , 
    // transaction isolation: read-committed, 
    // auto-commit false, read-only false
    assertConnectionState(
            ResultSet.CLOSE_CURSORS_AT_COMMIT, 
            Connection.TRANSACTION_READ_COMMITTED,
            false, false, conn5);
    sru5a.executeUpdate("insert into autocommitxastart values 6,7");
    rs5 = sru5a.executeQuery("select count(*) from autocommitxastart");
    rs5.next();
    assertEquals(7, rs5.getInt(1));
    xar.end(xid5a, XAResource.TMSUCCESS);
    xar.commit(xid5a, true);
    conn5.close();
    xac5.close();
    sru5a.close();
}
 
Example 17
Source File: XATransactionTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testGlobalXIDinTransactionTable() throws Exception {
    Statement stm = getConnection().createStatement();
    stm.execute("create table XATT2 (i int, text char(10))");

    XADataSource xaDataSource = J2EEDataSource.getXADataSource();
    XAConnection xaConn = xaDataSource.getXAConnection();
    XAResource xaRes = xaConn.getXAResource();
    Connection conn = xaConn.getConnection();

    // create large enough xid
    byte[] gid = new byte[64];
    byte[] bid = new byte[64];
    for (int i=0; i < 64; i++) {
        gid[i] = (byte) i;
        bid[i] = (byte) (64 - i);
    }
    Xid xid = new ClientXid(0x1234, gid, bid);

    // get the stuff required to execute the global transaction
    xaConn = xaDataSource.getXAConnection();
    xaRes = xaConn.getXAResource();
    conn = xaConn.getConnection();

    // start the transaction with that xid
    xaRes.start(xid, XAResource.TMNOFLAGS);

    // do some work
    stm = conn.createStatement();
    stm.execute("insert into XATT2 values (1234, 'Test_Entry')");
    stm.close();

    // end the wotk on the transaction branch
    xaRes.end(xid, XAResource.TMSUCCESS);

    ResultSet rs = null;
    stm = null;

    try {

        // check the output of the global xid in syscs_diag.transaction_table
        stm = getConnection().createStatement();

        String query = "select global_xid from syscs_diag.transaction_table"
                     + " where global_xid is not null";

        // execute the query to obtain the xid of the global transaction
        rs = stm.executeQuery(query);

        // there should be at least one globaltransaction in progress
        Assert.assertTrue(rs.next());

        // check whether the xid obtained matches the original xid
        Xid rXid = parseXid(rs.getString(1));
        Assert.assertEquals(xid, rXid);

        // there should be at most one global transaction in progress
        Assert.assertFalse(rs.next());

    } catch (Exception ex) {
        try {
            // close all the stuff
            if (rs != null)
                rs.close();
            if (stm != null)
                stm.close();

            // rollback the global transaction
            xaRes.rollback(xid);
            // close the connection
            xaConn.close();
        } catch (Exception e) {
            // ignore the exception because it
            // would hide the original exception
        }
        // throw the stuff further
        throw ex;
    }

    // close all the stuff
    rs.close();
    stm.close();

    // rollback the global transaction
    xaRes.rollback(xid);

    // close the connection
    xaConn.close();
}
 
Example 18
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testAutoCommitOnXAResourceStart() throws SQLException, XAException {

        XADataSource dsx = J2EEDataSource.getXADataSource();
        XAConnection xac4 = dsx.getXAConnection();
        Xid xid4a= null;

        // We get an XAID_DUP error from networkserver when attempting
        // the XAResource.start below if we use the same xid.
        // Possibly because we're in the same jvm.
        // When the test is run with clientserverSuite, rather than default,
        // this wasn't needed, so just create a different id for client
        if (usingEmbedded())
            xid4a = new cdsXid(4, (byte) 23, (byte) 76);
        else if (usingDerbyNetClient())
            xid4a = new cdsXid(5, (byte) 23, (byte) 76);
            
        Connection conn4 = xac4.getConnection();
        assertTrue(conn4.getAutoCommit());

        Statement s4 = conn4.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
        ResultSet rs4 = s4.executeQuery("select i from autocommitxastart");
        rs4.next();
        assertEquals(1, rs4.getInt(1));
        rs4.next();
        assertEquals(2, rs4.getInt(1));

        // XAResource().start should commit the transaction
        xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
        xac4.getXAResource().end(xid4a, XAResource.TMSUCCESS);

        try {
            rs4.next();
            fail ("expected an exception indicating resultset is closed.");
        } catch (SQLException sqle) {
            // Embedded gets 08003. No current connection (DERBY-2620)        	
        	if (usingDerbyNetClient())
        		assertSQLState("XCL16",sqle);
        }

        conn4.setAutoCommit(false);
        assertFalse(conn4.getAutoCommit());

        rs4 = s4.executeQuery("select i from autocommitxastart");
        rs4.next();
        assertEquals(1, rs4.getInt(1));
        rs4.next();
        assertEquals(2, rs4.getInt(1));
        
         // Get a new xid to begin another transaction. 
        if (usingEmbedded())
            xid4a = new cdsXid(4, (byte) 93, (byte) 103);
        else if (usingDerbyNetClient())
            xid4a = new cdsXid(5, (byte) 93, (byte) 103);

        try {
            xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
        } catch (XAException xae) {
            if (usingEmbedded())
                assertNull(xae.getMessage());
            else if (usingDerbyNetClient())
            {
                // This should give XAER_OUTSIDE exception because
                // the resource manager is busy in the local transaction
                assertTrue(xae.getMessage().indexOf("XAER_OUTSIDE") >=0 );
            }
            assertEquals(-9, xae.errorCode);
        }
        
        rs4.next();
        assertEquals(3, rs4.getInt(1));
        rs4.close();

        conn4.rollback();
        conn4.close();
        xac4.close();
    }
 
Example 19
Source File: xaHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void XADataSourceStatement(ij parser, Token dbname, Token shutdown,
								String create)
	 throws SQLException
{
	try
	{
		  currentXADataSource = (XADataSource) getXADataSource();

		  databaseName = parser.stringValue(dbname.image);
		  
		  if (isJCC || isNetClient)
		  {
		  	String hostName = System.getProperty("hostName");
		  	if ((hostName != null ) && (!hostName.equals("localhost")))
			{			
		  		xaHelper.setDataSourceProperty(currentXADataSource,
										 "ServerName", hostName);
			}
		  	else
			{			
		  		xaHelper.setDataSourceProperty(currentXADataSource,
						 "ServerName", "localhost");
			}
		  xaHelper.setDataSourceProperty(currentXADataSource,
										 "portNumber", 1527);
		  
		  String user;
		  String password;
		  user = "APP";
		  password = "APP";
		  xaHelper.setDataSourceProperty(currentXADataSource,
										 "user", user);
		  xaHelper.setDataSourceProperty(currentXADataSource,
										 "password", password);
		  //xaHelper.setDataSourceProperty(currentXADataSource,
		  //"traceFile", "trace.out." + framework);
		  }
		  if (isJCC)
		  {
			  xaHelper.setDataSourceProperty(currentXADataSource,
											 "driverType", 4);

			  xaHelper.setDataSourceProperty(currentXADataSource, 
											 "retrieveMessagesFromServerOnGetMessage", true);
		  }
		  xaHelper.setDataSourceProperty(currentXADataSource, "databaseName", databaseName);

		if (shutdown != null && shutdown.toString().toLowerCase(Locale.ENGLISH).equals("shutdown"))
		{	
			if (isJCC || isNetClient)
				xaHelper.setDataSourceProperty(currentXADataSource,"databaseName", databaseName + ";shutdown=true");
			else
				xaHelper.setDataSourceProperty(currentXADataSource, "shutdownDatabase", "shutdown");

			// do a getXAConnection to shut it down */
			currentXADataSource.getXAConnection().getConnection();
			currentXADataSource = null;
			currentXAConnection = null;
		}
		else if (create != null && create.toLowerCase(java.util.Locale.ENGLISH).equals("create"))
		{
			if (isJCC || isNetClient)
				xaHelper.setDataSourceProperty(currentXADataSource,"databaseName", databaseName + ";create=true");
			else
				xaHelper.setDataSourceProperty(currentXADataSource,
											   "createDatabase", "create");

			/* do a getXAConnection to create it */
			XAConnection conn = currentXADataSource.getXAConnection();
			conn.close();
			
			xaHelper.setDataSourceProperty(currentXADataSource, "createDatabase", null);
		}
	}
	catch (Throwable t)
	{
		handleException(t);
	}	
}
 
Example 20
Source File: ClosedObjectTest.java    From gemfirexd-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new connection using an <code>XADataSource</code>.
 *
 * @return a <code>Connection</code> value
 * @exception SQLException if an error occurs
 */
protected Connection newConnection_() throws SQLException {
    XADataSource ds = J2EEDataSource.getXADataSource();
    XAConnection xac = ds.getXAConnection();
    return xac.getConnection();
}