當前位置: 首頁>>代碼示例>>Java>>正文


Java JdbcDataSource.getXAConnection方法代碼示例

本文整理匯總了Java中org.h2.jdbcx.JdbcDataSource.getXAConnection方法的典型用法代碼示例。如果您正苦於以下問題:Java JdbcDataSource.getXAConnection方法的具體用法?Java JdbcDataSource.getXAConnection怎麽用?Java JdbcDataSource.getXAConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.h2.jdbcx.JdbcDataSource的用法示例。


在下文中一共展示了JdbcDataSource.getXAConnection方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testMixedXaNormal

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testMixedXaNormal() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test");
    ds.setUser("sa");
    ds.setPassword("");
    XAConnection xa = ds.getXAConnection();
    Connection c = xa.getConnection();
    assertTrue(c.getAutoCommit());
    MyXid xid = new MyXid();
    XAResource res = xa.getXAResource();

    res.start(xid, XAResource.TMNOFLAGS);
    assertTrue(!c.getAutoCommit());
    res.end(xid, XAResource.TMSUCCESS);
    res.commit(xid, true);
    assertTrue(c.getAutoCommit());

    res.start(xid, XAResource.TMNOFLAGS);
    assertTrue(!c.getAutoCommit());
    res.end(xid, XAResource.TMFAIL);
    res.rollback(xid);
    assertTrue(c.getAutoCommit());

    c.close();
    xa.close();
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:27,代碼來源:TestXA.java

示例2: testXAAutoCommit

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testXAAutoCommit() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test");
    ds.setUser("sa");
    ds.setPassword("");
    XAConnection xa = ds.getXAConnection();
    MyXid xid = new MyXid();
    xa.getXAResource().start(xid,
            XAResource.TMNOFLAGS);
    Connection c = xa.getConnection();
    assertTrue(!c.getAutoCommit());
    c.close();
    xa.close();
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:15,代碼來源:TestXA.java

示例3: testRollbackWithoutPrepare

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testRollbackWithoutPrepare() throws Exception {
    if (config.memory) {
        return;
    }
    Xid xid = new Xid() {
        @Override
        public int getFormatId() {
            return 3145;
        }
        @Override
        public byte[] getGlobalTransactionId() {
            return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
        }
        @Override
        public byte[] getBranchQualifier() {
            return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
        }
    };
    deleteDb("xa");
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(getURL("xa", true));
    ds.setPassword(getPassword());
    Connection dm = ds.getConnection();
    Statement stat = dm.createStatement();
    stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
    stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
    dm.close();
    XAConnection c = ds.getXAConnection();
    XAResource xa = c.getXAResource();
    Connection connection = c.getConnection();
    xa.start(xid, XAResource.TMJOIN);
    PreparedStatement ps = connection.prepareStatement(
            "UPDATE TEST SET VAL=? WHERE ID=?");
    ps.setInt(1, new Random().nextInt());
    ps.setInt(2, 1);
    ps.close();
    xa.rollback(xid);
    connection.close();
    c.close();
    deleteDb("xa");
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:42,代碼來源:TestXA.java

示例4: testTwoPhase

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testTwoPhase(String db, boolean shutdown, boolean commit)
        throws Exception {
    deleteDb(db);
    JdbcDataSource ds = new JdbcDataSource();
    ds.setPassword(getPassword());
    ds.setUser("sa");
    // ds.setURL(getURL("xaSimple", true) + ";trace_level_system_out=3");
    ds.setURL(getURL(db, true));

    XAConnection xa;
    xa = ds.getXAConnection();
    Connection conn;

    conn = xa.getConnection();
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar(255))");
    Xid xid = SimpleXid.createRandom();
    xa.getXAResource().start(xid, XAResource.TMNOFLAGS);
    conn.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    xa.getXAResource().end(xid, XAResource.TMSUCCESS);
    xa.getXAResource().prepare(xid);
    if (shutdown) {
        shutdown(ds);
    }

    xa = ds.getXAConnection();
    Xid[] list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
    assertEquals(1, list.length);
    assertTrue(xid.equals(list[0]));
    if (commit) {
        xa.getXAResource().commit(list[0], false);
    } else {
        xa.getXAResource().rollback(list[0]);
    }
    conn = xa.getConnection();
    conn.createStatement().executeQuery("select * from test");
    if (shutdown) {
        shutdown(ds);
    }

    xa = ds.getXAConnection();
    list = xa.getXAResource().recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    conn = xa.getConnection();
    ResultSet rs;
    rs = conn.createStatement().executeQuery("select * from test");
    if (commit) {
        assertTrue(rs.next());
    } else {
        assertFalse(rs.next());
    }
    xa.close();
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:55,代碼來源:TestXASimple.java

示例5: testSimple

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
private void testSimple() throws SQLException {

        deleteDb("xaSimple1");
        deleteDb("xaSimple2");
        org.h2.Driver.load();

        // InitialContext context = new InitialContext();
        // context.rebind(USER_TRANSACTION_JNDI_NAME, j.getUserTransaction());

        JdbcDataSource ds1 = new JdbcDataSource();
        ds1.setPassword(getPassword());
        ds1.setUser("sa");
        ds1.setURL(getURL("xaSimple1", true));

        JdbcDataSource ds2 = new JdbcDataSource();
        ds2.setPassword(getPassword());
        ds2.setUser("sa");
        ds2.setURL(getURL("xaSimple2", true));

        // UserTransaction ut = (UserTransaction)
        // context.lookup("UserTransaction");
        // ut.begin();

        XAConnection xa1 = ds1.getXAConnection();
        Connection c1 = xa1.getConnection();
        c1.setAutoCommit(false);
        XAConnection xa2 = ds2.getXAConnection();
        Connection c2 = xa2.getConnection();
        c2.setAutoCommit(false);

        c1.createStatement().executeUpdate(
                "create table test(id int, test varchar(255))");
        c2.createStatement().executeUpdate(
                "create table test(id int, test varchar(255))");

        // ut.rollback();
        c1.close();
        c2.close();

        xa1.close();
        xa2.close();

        // j.stop();
        // System.exit(0);
        deleteDb("xaSimple1");
        deleteDb("xaSimple2");

    }
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:49,代碼來源:TestXASimple.java

示例6: simpleResource

import org.h2.jdbcx.JdbcDataSource; //導入方法依賴的package包/類
@Test
public void simpleResource() throws SQLException, XAException {
	final JdbcDataSource ds = new JdbcDataSource();
	ds.setURL(DatabaseUtil.URL_H2);
	ds.setUser(DatabaseUtil.USER);
	ds.setPassword(DatabaseUtil.PASSWORD);

	final XAConnection xaConnection = ds.getXAConnection();

	Connection connection = null;

	try {
		connection = xaConnection.getConnection();
		DatabaseUtil.createDatabase(connection);

		final Statement statement = connection.createStatement();

		final Xid xid = new SimpleXid(100, new byte[] { 0x01 }, new byte[] { 0x02 });

		final XAResource xaResource = xaConnection.getXAResource();

		xaResource.start(xid, XAResource.TMNOFLAGS);

		statement.execute(
				"INSERT INTO PERSONNE (PRENOM, NOM, DATE_NAISSANCE) VALUES ('RollBack1', 'SQL', '1970-01-01');");

		statement.execute(
				"INSERT INTO PERSONNE (PRENOM, NOM, DATE_NAISSANCE) VALUES ('RollBack2', 'SQL', '1970-01-01');");

		statement.execute(
				"INSERT INTO PERSONNE (PRENOM, NOM, DATE_NAISSANCE) VALUES ('RollBack3', 'SQL', '1970-01-01');");

		statement.execute(
				"INSERT INTO PERSONNE (PRENOM, NOM, DATE_NAISSANCE) VALUES ('RollBack4', 'SQL', '1970-01-01');");

		xaResource.end(xid, XAResource.TMSUCCESS);

		final int prepare = xaResource.prepare(xid);
		if (prepare == XAResource.XA_OK) {
			xaResource.commit(xid, false);
		}
	} finally {
		if (connection != null) {
			connection.close();
		}
	}
}
 
開發者ID:marcosemiao,項目名稱:log4jdbc,代碼行數:48,代碼來源:XATransaction.java


注:本文中的org.h2.jdbcx.JdbcDataSource.getXAConnection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。