本文整理匯總了Java中java.sql.Connection.commit方法的典型用法代碼示例。如果您正苦於以下問題:Java Connection.commit方法的具體用法?Java Connection.commit怎麽用?Java Connection.commit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.Connection
的用法示例。
在下文中一共展示了Connection.commit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: updateNodeHeartbeat
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Method to update the node heartbeat value to the current time.
*
* @param nodeId local node ID
* @return true if the update is successful, false otherwise
* @throws CoordinationException if an error is detected while updating the node heartbeat.
*/
public boolean updateNodeHeartbeat(String nodeId) throws CoordinationException {
Connection connection = null;
PreparedStatement preparedStatementForNodeUpdate = null;
String task = RdbmsCoordinationConstants.TASK_UPDATE_NODE_HEARTBEAT;
try {
connection = getConnection();
preparedStatementForNodeUpdate = connection.prepareStatement(
RdbmsCoordinationConstants.PS_UPDATE_NODE_HEARTBEAT);
preparedStatementForNodeUpdate.setLong(1, System.currentTimeMillis());
preparedStatementForNodeUpdate.setString(2, nodeId);
int updateCount = preparedStatementForNodeUpdate.executeUpdate();
connection.commit();
return updateCount != 0;
} catch (SQLException e) {
rollback(connection, task);
throw new CoordinationException("Error occurred while " + task + ". Node ID: " + nodeId, e);
} finally {
close(preparedStatementForNodeUpdate, task);
close(connection, task);
}
}
示例2: testLifecyleInterceptor
import java.sql.Connection; //導入方法依賴的package包/類
public void testLifecyleInterceptor() throws Exception {
createTable("testLifecycleInterceptor", "(field1 int)", "InnoDB");
Connection liConn = null;
try {
liConn = getConnectionWithProps("connectionLifecycleInterceptors=testsuite.simple.TestLifecycleInterceptor");
liConn.setAutoCommit(false);
liConn.createStatement().executeUpdate("INSERT INTO testLifecycleInterceptor VALUES (1)");
liConn.commit();
assertEquals(TestLifecycleInterceptor.transactionsBegun, 1);
assertEquals(TestLifecycleInterceptor.transactionsCompleted, 1);
liConn.createStatement().execute("SELECT * FROM testLifecycleInterceptor");
assertEquals(TestLifecycleInterceptor.transactionsBegun, 2);
// implicit commit
liConn.createStatement().executeUpdate("CREATE TABLE testLifecycleFoo (field1 int)");
assertEquals(TestLifecycleInterceptor.transactionsCompleted, 2);
} finally {
if (liConn != null) {
liConn.createStatement().executeUpdate("DROP TABLE IF EXISTS testLifecycleFoo");
liConn.close();
}
}
}
示例3: persist
import java.sql.Connection; //導入方法依賴的package包/類
@Override
public void persist(Queue queue) throws BrokerException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection();
statement = connection.prepareStatement(RDBMSConstants.PS_INSERT_QUEUE);
statement.setString(1, queue.getName());
statement.setBytes(2, new byte[0]);
statement.executeUpdate();
connection.commit();
} catch (SQLException e) {
throw new BrokerException("Error occurred while storing queue " + queue, e);
} finally {
close(connection, statement);
}
}
示例4: testFindWithClassificationMapperDomainAndCategory
import java.sql.Connection; //導入方法依賴的package包/類
@Test
public void testFindWithClassificationMapperDomainAndCategory()
throws NotAuthorizedException, SQLException, ClassificationAlreadyExistException,
ClassificationNotFoundException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification1 = this.createNewClassificationWithUniqueKey("domain1", "t1");
classification1.setCategory("category1");
classificationService.createClassification(classification1);
Classification classification2 = this.createNewClassificationWithUniqueKey("domain2", "t1");
classification2.setCategory("category1");
classificationService.createClassification(classification2);
Classification classification3 = this.createNewClassificationWithUniqueKey("domain1", "t1");
classification3.setCategory("category2");
classificationService.createClassification(classification3);
List<ClassificationSummary> list = classificationService.createClassificationQuery()
.category("category1")
.domain("domain1")
.list();
Assert.assertEquals(1, list.size());
list = classificationService.createClassificationQuery().domain("domain1", "domain3").list();
Assert.assertEquals(2, list.size());
connection.commit();
}
示例5: getConnection
import java.sql.Connection; //導入方法依賴的package包/類
@Override
protected Connection getConnection() {
try {
Connection conn = dbConf.getConnection();
conn.setAutoCommit(false);
PreparedStatement stmt =
conn.prepareStatement("SET extra_float_digits TO 0");
stmt.executeUpdate();
conn.commit();
return conn;
} catch (SQLException sqlE) {
LOG.error("Could not get connection to test server: " + sqlE);
return null;
} catch (ClassNotFoundException cnfE) {
LOG.error("Could not find driver class: " + cnfE);
return null;
}
}
開發者ID:aliyun,項目名稱:aliyun-maxcompute-data-collectors,代碼行數:19,代碼來源:DirectPostgreSQLExportManualTest.java
示例6: loadDatabase
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Invoke this benchmark's database loader using the given Connection handle
* @param conn
*/
protected final void loadDatabase(Connection conn, boolean calibrate, boolean generateFiles, String filesPath) {
try {
Loader loader = this.makeLoaderImpl(conn, calibrate,generateFiles,filesPath);
if (loader != null) {
conn.setAutoCommit(false);
loader.load();
conn.commit();
if (loader.getTableCounts().isEmpty() == false) {
LOG.info("Table Counts:\n" + loader.getTableCounts());
}
}
} catch (SQLException ex) {
throw new RuntimeException(String.format("Unexpected error when trying to load the %s database", this.benchmarkName), ex);
}
}
示例7: testSequnce
import java.sql.Connection; //導入方法依賴的package包/類
private static void testSequnce(Connection theCon) throws SQLException {
try {
theCon.setAutoCommit(false);
String sql = "select next value for MYCATSEQ_GLOBAL ";
Statement stmt = theCon.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println(Thread.currentThread().getName()
+ " get seq " + rs.getLong(1));
} else {
System.out.println(Thread.currentThread().getName()
+ " can't get seq ");
}
theCon.commit();
stmt.close();
} finally {
theCon.close();
}
}
示例8: insertFunction
import java.sql.Connection; //導入方法依賴的package包/類
private static void insertFunction(int id, String msg,
SetExtraArgs setExtraArgs) throws SQLException {
instanceForProcedure.functionCalls += 1;
Connection con = instanceForProcedure.getConnection();
StringBuilder sql = new StringBuilder("insert into ");
sql.append(instanceForProcedure.getTableName());
sql.append("(id, msg");
for (int i = 0; i < instanceForProcedure.names.length; ++i) {
sql.append(", ");
sql.append(instanceForProcedure.names[i]);
}
sql.append(") values (");
for (int i = 0; i < instanceForProcedure.names.length + 2; ++i) {
sql.append("?,");
}
// Remove last ,
sql = sql.delete(sql.length() - 1, sql.length());
sql.append(")");
PreparedStatement statement = con.prepareStatement(sql.toString());
try {
statement.setInt(1, id);
statement.setString(2, msg);
setExtraArgs.set(statement);
statement.execute();
con.commit();
} finally {
statement.close();
}
}
示例9: testBug31053
import java.sql.Connection; //導入方法依賴的package包/類
public void testBug31053() throws Exception {
Properties props = new Properties();
props.setProperty("connectTimeout", "2000");
props.setProperty("loadBalanceStrategy", "random");
Connection lbConn = getLoadBalancedConnection(2, "localhost:23", props);
lbConn.setAutoCommit(false);
for (int i = 0; i < 10; i++) {
lbConn.commit();
}
}
示例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();
}
示例11: dropTableIfExists
import java.sql.Connection; //導入方法依賴的package包/類
@Override
protected void dropTableIfExists(String table) throws SQLException {
Connection conn = getManager().getConnection();
PreparedStatement statement = conn.prepareStatement(
"DROP TABLE IF EXISTS "
+ table, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
try {
statement.executeUpdate();
conn.commit();
} finally {
statement.close();
}
}
示例12: dropTableIfExists
import java.sql.Connection; //導入方法依賴的package包/類
/**
* Drop a table if it already exists in the database.
*
* @param table
* the name of the table to drop.
* @throws SQLException
* if something goes wrong.
*/
protected void dropTableIfExists(String table) throws SQLException {
Connection conn = getManager().getConnection();
String sqlStmt = "IF OBJECT_ID('" + table
+ "') IS NOT NULL DROP TABLE " + table;
PreparedStatement statement = conn.prepareStatement(sqlStmt,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
try {
statement.executeUpdate();
conn.commit();
} finally {
statement.close();
}
}
開發者ID:aliyun,項目名稱:aliyun-maxcompute-data-collectors,代碼行數:23,代碼來源:SQLServerParseMethodsManualTest.java
示例13: testFindWithClassificationMapperCustomAndCategory
import java.sql.Connection; //導入方法依賴的package包/類
@Test
public void testFindWithClassificationMapperCustomAndCategory()
throws NotAuthorizedException, SQLException, ClassificationAlreadyExistException,
ClassificationNotFoundException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification1 = this.createNewClassificationWithUniqueKey("", "t1");
classification1.setDescription("DESC1");
classification1.setCategory("category1");
classificationService.createClassification(classification1);
Classification classification2 = this.createNewClassificationWithUniqueKey("", "t1");
classification2.setDescription("DESC1");
classification2.setCustom1("custom1");
classification2.setCategory("category1");
classificationService.createClassification(classification2);
Classification classification3 = this.createNewClassificationWithUniqueKey("", "t1");
classification3.setCustom1("custom2");
classification3.setCustom2("custom1");
classification3.setCategory("category2");
classificationService.createClassification(classification3);
Classification classification4 = this.createNewClassificationWithUniqueKey("", "t1");
classification4.setDescription("description2");
classification4.setCustom8("custom2");
classification4.setCategory("category1");
classificationService.createClassification(classification4);
List<ClassificationSummary> list = classificationService.createClassificationQuery()
.descriptionLike("DESC1")
.customFields("custom1")
.list();
Assert.assertEquals(1, list.size());
list = classificationService.createClassificationQuery().customFields("custom2").list();
Assert.assertEquals(2, list.size());
list = classificationService.createClassificationQuery().descriptionLike("DESC1").category("category1").list();
Assert.assertEquals(2, list.size());
connection.commit();
}
示例14: close
import java.sql.Connection; //導入方法依賴的package包/類
public static void close(Connection connection) {
try {
if (connection != null) {
if (!connection.getAutoCommit())
connection.commit();
connection.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
示例15: testBug56429
import java.sql.Connection; //導入方法依賴的package包/類
public void testBug56429() throws Exception {
Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
props.setProperty("autoReconnect", "true");
props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");
Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);
String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);
props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");
props.setProperty("queriesBeforeRetryMaster", "50");
props.setProperty("maxReconnects", "1");
UnreliableSocketFactory.mapHost("master", host);
UnreliableSocketFactory.mapHost("slave", host);
Connection failoverConnection = null;
try {
failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
String userHost = getSingleIndexedValueWithQuery(1, "SELECT USER()").toString();
String[] userParts = userHost.split("@");
this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");
int startConnCount = 0;
while (this.rs.next()) {
if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
startConnCount++;
}
}
assert (startConnCount > 0);
failoverConnection.setAutoCommit(false); // this will fail if state
// not copied over
for (int i = 0; i < 20; i++) {
failoverConnection.commit();
}
this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");
int endConnCount = 0;
while (this.rs.next()) {
if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
endConnCount++;
}
}
assert (endConnCount > 0);
if (endConnCount - startConnCount >= 20) { // this may be bogus if run on a real system, we should probably look to see they're coming from this
// testsuite?
fail("We're leaking connections even when not failed over");
}
} finally {
if (failoverConnection != null) {
failoverConnection.close();
}
}
}