当前位置: 首页>>代码示例>>Java>>正文


Java Connection.setAutoCommit方法代码示例

本文整理汇总了Java中java.sql.Connection.setAutoCommit方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.setAutoCommit方法的具体用法?Java Connection.setAutoCommit怎么用?Java Connection.setAutoCommit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.sql.Connection的用法示例。


在下文中一共展示了Connection.setAutoCommit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: upgrade

import java.sql.Connection; //导入方法依赖的package包/类
/**
 * Perform upgrade.
 */
public void upgrade(final Connection conn, final int upgradeToVersion) throws SQLException {
  final boolean savedAutoCommit = conn.getAutoCommit();
  Statement st = null; // NOPMD
  try {
    // create statement
    conn.setAutoCommit(true);
    st = conn.createStatement();

    LOG.debug("Altering table");
    PersistanceUtils.executeDDLs(st, new String[]{
            " alter table TEST_CASE drop constraint TEST_CASE_UC2",
            " alter table TEST_CASE add constraint TEST_CASE_UC2 unique (TEST_PACKAGE_ID, NAME)",
    });

    LOG.debug("Updating version");
    st.executeUpdate("update SYSTEM_PROPERTY set VALUE = '" + upgraderVersion() + "' where NAME = 'parabuild.schema.version' ");

    // finish
    conn.commit();

  } finally {
    IoUtils.closeHard(st);
    conn.setAutoCommit(savedAutoCommit);
  }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:29,代码来源:UpgraderToVersion65.java

示例2: testBug51776

import java.sql.Connection; //导入方法依赖的package包/类
public void testBug51776() throws Exception {
    Properties props = getHostFreePropertiesFromTestsuiteUrl();
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    NonRegisteringDriver d = new NonRegisteringDriver();
    Properties parsed = d.parseURL(BaseTestCase.dbUrl, null);
    String db = parsed.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    String port = parsed.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);
    String host = getPortFreeHostname(props, d);

    UnreliableSocketFactory.flushAllStaticData();
    UnreliableSocketFactory.mapHost("first", host);

    Connection testConn = getConnectionWithProps("jdbc:mysql://first:" + port + "/" + db, props);
    testConn.setAutoCommit(false);
    testConn.createStatement().execute("SELECT 1");
    UnreliableSocketFactory.downHost("first");
    try {
        testConn.rollback();
        fail("Should receive SQLException on rollback().");
    } catch (SQLException e) {

    }

}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:26,代码来源:StatementRegressionTest.java

示例3: flush

import java.sql.Connection; //导入方法依赖的package包/类
public void flush(final List<String> sqlBatch) throws SQLException {
    if (sqlBatch == null || sqlBatch.isEmpty()) {
        return;
    }
    Connection connection = connectionHolder.getValidConnection();
    connection.setAutoCommit(false);

    Statement statement = connection.createStatement();
    for (String sql : sqlBatch) {
        log.info("===>>>statement addBatch sql:{}", sql);
        statement.addBatch(sql);
    }
    int[] updateCountArr = statement.executeBatch();
    if (updateCountArr.length != sqlBatch.size()) {
        throw new ConnectException(String.format("updateCountArr size:(%d) not equals to sqlBatch size:(%d)", updateCountArr.length, sqlBatch.size()));
    }
    connection.commit();
    statement.close();
}
 
开发者ID:songxin1990,项目名称:maxwell-sink,代码行数:20,代码来源:MySqlDbWriter.java

示例4: getConnection

import java.sql.Connection; //导入方法依赖的package包/类
/**
 * Make sure to close after using the returned connection
 * (like in a finally block).
 */
protected Connection getConnection(String id) throws SQLException {

    Connection c = DriverManager.getConnection("jdbc:hsqldb:file:"
        + baseDir.getAbsolutePath() + '/' + id + "/dbfile", "SA", "");

    if (verbose) {
        System.err.println("Opening JDBC URL '" + "jdbc:hsqldb:file:"
                           + baseDir.getAbsolutePath() + '/' + id
                           + "/dbfile");
    }

    c.setAutoCommit(false);

    return c;
}
 
开发者ID:Julien35,项目名称:dev-courses,代码行数:20,代码来源:TestDbBackup.java

示例5: newConnection

import java.sql.Connection; //导入方法依赖的package包/类
/**
 * Get a connection from URL. Initialized with <code>connectionsProperties</code>. Set autoCommit to false in case of UPDATE.
 * @param context
 * @param connectionURL
 * @param connectionsProperties
 * @param autoCommit
 * @return
 * @throws SQLException
 */
@VisibleForTesting
Connection newConnection(final Context context, final String connectionURL, final Properties connectionsProperties,
		boolean autoCommit)
		throws SQLException {
	final Connection conn = DriverManager.getConnection(connectionURL, connectionsProperties);
	conn.setAutoCommit(autoCommit);
	context.getLogger().info("Connected to database: " + connectionURL);
	return conn;
}
 
开发者ID:Neotys-Labs,项目名称:Database-Advanced-Actions,代码行数:19,代码来源:SPExecuteActionEngineforgenericsp.java

示例6: run

import java.sql.Connection; //导入方法依赖的package包/类
@Override
public void run() {
	Connection con = null;
	try {

		List<String> batch = getNextBatch();
		while (!batch.isEmpty()) {
			try {
				if (con == null || con.isClosed()) {
					con = conPool.getConnection();
					if (con.getAutoCommit() != autocommit) {
						con.setAutoCommit(autocommit);
					}
				}

				insert(con, batch);
				finshiedCount.addAndGet(batch.size());
			} catch (Exception e) {
				System.out.println("caught err in  thread :"
						+ Thread.currentThread().getId() + " " + e);
				try {
					con.rollback();
				} catch (SQLException e1) {
					System.out.println("caught err in thread :"
							+ Thread.currentThread().getId()
							+ " rollback err " + e1);
				}
				failedCount.addAndGet(batch.size());
			}
			batch = getNextBatch();
		}
	} finally {
		if (con != null) {
			this.conPool.returnCon(con);
		}
	}

}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:39,代码来源:UserTableInsertJob.java

示例7: tearDown

import java.sql.Connection; //导入方法依赖的package包/类
@After
public void tearDown() throws Exception {
    final Connection c = this.dataSource.getConnection();
    final Statement s = c.createStatement();
    c.setAutoCommit(true);

    for (int i = 0; i < 5; i++) {
        final String sql = String.format("delete from casusers;");
        s.execute(sql);
    }
    c.close();
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:13,代码来源:QueryDatabaseAuthenticationHandlerTests.java

示例8: run

import java.sql.Connection; //导入方法依赖的package包/类
@Override
public void run() {
    Connection con = null;
    try {

        List<Map<String, String>> batch = getNextBatch();
        while (!batch.isEmpty()) {
            try {
                if (con == null || con.isClosed()) {
                    con = conPool.getConnection();
                    con.setAutoCommit(true);
                }

                insert(con, batch);
                finshiedCount.addAndGet(batch.size());
            } catch (Exception e) {
                failedCount.addAndGet(batch.size());
                e.printStackTrace();
            }
            batch = getNextBatch();
        }
    } finally {
        if (con != null) {
            this.conPool.returnCon(con);
        }
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:28,代码来源:GoodsInsertJob.java

示例9: setDefaults

import java.sql.Connection; //导入方法依赖的package包/类
public void setDefaults(Connection connection) throws SQLException {

        connection.setHoldability(this.holdability);

        if (this.transactionIsolation != Connection.TRANSACTION_NONE) {
            connection.setTransactionIsolation(this.transactionIsolation);
        }
        connection.setAutoCommit(this.isAutoCommit);
        connection.setReadOnly(this.isReadOnly);
        connection.setCatalog(this.catalog);
    }
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:12,代码来源:ConnectionDefaults.java

示例10: doSomeWork

import java.sql.Connection; //导入方法依赖的package包/类
private static void doSomeWork() throws SQLException {

        Connection conn = getConnection();
        Statement  stmt = conn.createStatement();

        conn.setAutoCommit(false);
        stmt.execute("INSERT INTO trig_test VALUES (1, 'hello')");
        stmt.execute("INSERT INTO trig_test VALUES (2, 'now what?')");
        stmt.execute("INSERT INTO trig_test VALUES (3, 'unchangable')");
        stmt.execute("INSERT INTO trig_test VALUES (4, 'goodbye')");
        conn.commit();
        dumpTable("trig_test");
        stmt.execute("UPDATE trig_test SET value = 'all done'");
        conn.commit();
        dumpTable("trig_test");
        stmt.execute("DELETE FROM trig_test");
        conn.rollback();
        dumpTable("trig_test");

        try {
            stmt.execute("INSERT INTO trig_test VALUES(11, 'whatever')");
        } catch (SQLException se) {
            se.printStackTrace();
        }

        stmt.execute("INSERT INTO trig_test VALUES(10, 'whatever')");
        conn.commit();
        dumpTable("trig_test");
        stmt.close();
        conn.close();
    }
 
开发者ID:Julien35,项目名称:dev-courses,代码行数:32,代码来源:TriggerSample.java

示例11: getConnection

import java.sql.Connection; //导入方法依赖的package包/类
public static Connection getConnection(String poolName) throws Exception {
	Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + poolName);
	/*
	switch (getConfigInfo(poolName).DB_TYPE){
		case Constant.DB_TYPE.ASE :
			conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
	}
	*/
	conn.setAutoCommit(false);
	
	return conn;
}
 
开发者ID:experdb,项目名称:eXperDB-DB2PG,代码行数:13,代码来源:DBCPPoolManager.java

示例12: DatabaseUtil

import java.sql.Connection; //导入方法依赖的package包/类
public DatabaseUtil(String dbfile) {
	Connection c = null;
      
	try {
		Class.forName("org.sqlite.JDBC");
		c = DriverManager.getConnection("jdbc:sqlite:" + dbfile);
		c.setAutoCommit(false);
	} catch ( Exception e ) {
		e.printStackTrace();
	}
	this.connection = c;
	System.out.println("Opened database successfully");
}
 
开发者ID:GigaGamma,项目名称:SuperiorCraft,代码行数:14,代码来源:DatabaseUtil.java

示例13: remove

import java.sql.Connection; //导入方法依赖的package包/类
protected boolean remove(Object obj, Connection conn) {

		Class clz = obj.getClass();

		String sql = MapperFactory.getSql(clz, Mapper.REMOVE);

		boolean flag = false;
		boolean isNoBizTx = false;

		PreparedStatement pstmt = null;
		try {

			conn.setAutoCommit(false);
			pstmt = conn.prepareStatement(sql);

			isNoBizTx = Tx.isNoBizTx();
			if (!isNoBizTx) {
				Tx.add(pstmt);
			}

			Parsed parsed = Parser.get(clz);

			int i = 1;

			SqlUtil.adpterSqlKey(pstmt, parsed.getKeyField(X.KEY_ONE), null, obj, i);

			flag = pstmt.executeUpdate() == 0 ? false : true;

			if (isNoBizTx) {
				conn.commit();
			}

		} catch (Exception e) {
			e.printStackTrace();
			if (isNoBizTx) {
				try {
					conn.rollback();
					System.out.println("line 334 " + e.getMessage());
					e.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			} else {
				throw new RollbackException("RollbackException: " + e.getMessage());
			}
		} finally {
			if (isNoBizTx) {
				close(pstmt);
				close(conn);
			}
		}

		return flag;
	}
 
开发者ID:x7-framework,项目名称:x7,代码行数:55,代码来源:DaoImpl.java

示例14: testBug51643

import java.sql.Connection; //导入方法依赖的package包/类
public void testBug51643() throws Exception {
    Properties props = new Properties();
    props.setProperty("loadBalanceStrategy", "com.mysql.jdbc.SequentialBalanceStrategy");

    Connection lbConn = getUnreliableLoadBalancedConnection(new String[] { "first", "second" }, props);
    try {
        PreparedStatement cPstmt = lbConn.prepareStatement("SELECT connection_id()");
        PreparedStatement serverPstmt = lbConn.prepareStatement("SELECT connection_id()");
        Statement plainStmt = lbConn.createStatement();

        lbConn.setAutoCommit(false);
        this.rs = cPstmt.executeQuery();
        this.rs.next();
        String cPstmtConnId = this.rs.getString(1);

        this.rs = serverPstmt.executeQuery();
        this.rs.next();
        String serverPstmtConnId = this.rs.getString(1);

        this.rs = plainStmt.executeQuery("SELECT connection_id()");
        this.rs.next();
        String plainStmtConnId = this.rs.getString(1);
        lbConn.commit();
        lbConn.setAutoCommit(false);

        this.rs = cPstmt.executeQuery();
        this.rs.next();
        String cPstmtConnId2 = this.rs.getString(1);
        assertFalse(cPstmtConnId2.equals(cPstmtConnId));

        this.rs = serverPstmt.executeQuery();
        this.rs.next();
        String serverPstmtConnId2 = this.rs.getString(1);
        assertFalse(serverPstmtConnId2.equals(serverPstmtConnId));

        this.rs = plainStmt.executeQuery("SELECT connection_id()");
        this.rs.next();
        String plainStmtConnId2 = this.rs.getString(1);
        assertFalse(plainStmtConnId2.equals(plainStmtConnId));
    } finally {
        lbConn.close();
    }
}
 
开发者ID:Jugendhackt,项目名称:OpenVertretung,代码行数:44,代码来源:ConnectionRegressionTest.java

示例15: doMigrations

import java.sql.Connection; //导入方法依赖的package包/类
/**
 * Performs the application migration process in one go
 *
 * @param context the database context to run the patches in
 * @return the number of patches applied
 * @throws SQLException       if an unrecoverable database error occurs while working with the patches table.
 * @throws MigrationException if an unrecoverable error occurs during the migration
 */
protected int doMigrations(JdbcMigrationContext context) throws SQLException, MigrationException
{
    PatchInfoStore patchTable = createPatchStore(context);

    lockPatchStore(context);

    // Now apply the patches
    int executedPatchCount = 0;
    try
    {

        // remember the auto-commit state, and turn auto-commit off
        Connection conn = context.getConnection();
        boolean commitState = conn.getAutoCommit();
        conn.setAutoCommit(false);

        // run the migrations
        try
        {
            executedPatchCount = migrationProcess.doMigrations(patchTable,
                    context);
        }

        // restore autocommit state
        finally
        {
            if ((conn != null) && !conn.isClosed())
            {
                conn.setAutoCommit(commitState);
            }
        }
    }
    catch (MigrationException me)
    {
        // If there was any kind of error, we don't want to eat it, but we do
        // want to unlock the patch store. So do that, then re-throw.
        patchTable.unlockPatchStore();
        throw me;
    }

    // Do any post-patch tasks
    try
    {
        migrationProcess.doPostPatchMigrations(context);
        return executedPatchCount;
    }
    finally
    {
        try
        {
            patchTable.unlockPatchStore();
        }
        catch (MigrationException e)
        {
            log.error("Error unlocking patch table: ", e);
        }
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:67,代码来源:JdbcMigrationLauncher.java


注:本文中的java.sql.Connection.setAutoCommit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。