private PreparedStatement getPreparedStmt(Connection conn) throws SQLException { conn.setAutoCommit(false); //return conn.prepareStatement("UPDATE " + tableName + " SET patterns = ? WHERE sentid = ? and tokenid = ?; " + // "INSERT INTO " + tableName + " (sentid, tokenid, patterns) (SELECT ?,?,? WHERE NOT EXISTS (SELECT sentid FROM " + tableName + " WHERE sentid =? and tokenid=?));"); // return conn.prepareStatement("INSERT INTO " + tableName + " (sentid, tokenid, patterns) (SELECT ?,?,? WHERE NOT EXISTS (SELECT sentid FROM " + tableName + " WHERE sentid =? and tokenid=?))"); return conn.prepareStatement("select upsert_patterns(?,?)"); }
public void prepare(boolean needsAutoCommit) throws SQLException { toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit(); if ( toggleAutoCommit ) { try { connection.commit(); } catch( Throwable ignore ) { // might happen with a managed connection } connection.setAutoCommit( true ); } }
public void executeBatch(List<Entity> entities) throws SQLException { try (Connection connection = dataSource.getConnection()) { connection.setAutoCommit(false); try (PreparedStatement statement = connection.prepareStatement(SQL)) { // ... try { connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } } }
/** * @throws Exception if failed. */ @Test public void testCommitIgnoredWhenMvccIsDisabled() throws Exception { try (Connection conn = DriverManager.getConnection(URL)) { conn.setAutoCommit(false); conn.createStatement().execute("COMMIT"); conn.commit(); } // assert no exception }
public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people;"); stat.executeUpdate("create table people (name, occupation);"); PreparedStatement prep = conn.prepareStatement( "insert into people values (?, ?);"); prep.setString(1, "Gandhi"); prep.setString(2, "politics"); prep.addBatch(); prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch(); prep.addBatch(); conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("job = " + rs.getString("occupation")); conn.close();
@Test public void testNoKVColumn() throws Exception { long ts = nextTimestamp(); ensureTableCreated(getUrl(),BTABLE_NAME,null, ts-2); String url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts; // Insert at timestamp 0 Properties props = new Properties(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(url, props); conn.setAutoCommit(true); PreparedStatement stmt = conn.prepareStatement( "upsert into BTABLE VALUES (?, ?, ?, ?, ?)"); stmt.setString(1, "abc"); stmt.setString(2, "123"); stmt.setString(3, "x"); stmt.setInt(4, 1); stmt.setString(5, "ab"); // Succeeds since we have an empty KV stmt.execute(); }
connection.setAutoCommit(false); statement = connection.createStatement(); if (!rs.next()) { logger.info("Database not built for repository: " + databaseUrl + ". Building now..."); RepositoryUtils.closeQuietly(rs); connection.commit(); } catch (SQLException sqle) { RepositoryUtils.rollback(connection, logger);
@Test public void restoreAutoCommitInitialStateOnUnexpectedError() throws Exception { when(connection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(statement); when(statement.execute()).thenReturn(true); when(statement.getUpdateCount()).thenReturn(1); doThrow(new SQLException("infrastructure error")).when(connection).commit(); inOrder.verify(connection).setAutoCommit(false); inOrder.verify(connection).prepareStatement("insert into something (id, name) values (?, ?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); inOrder.verify(statement).execute(); inOrder.verify(statement).getUpdateCount(); inOrder.verify(connection).commit(); inOrder.verify(connection).setAutoCommit(true);
@Test public void testTransactionWithExceptionOnRollback() throws Exception { given(con.getAutoCommit()).willReturn(true); willThrow(new SQLException("Cannot rollback")).given(con).rollback(); TransactionTemplate tt = new TransactionTemplate(tm); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { status.setRollbackOnly(); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).rollback(); ordered.verify(con).setAutoCommit(true); verify(con).close(); }
/** * @throws Exception If failed. */ @Test public void testTxAllowedRollback() throws Exception { String url = CFG_URL_PREFIX + "transactionsAllowed=true@" + configURL(); try (final Connection conn = DriverManager.getConnection(url)) { conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn.getTransactionIsolation()); conn.setAutoCommit(false); conn.rollback(); } }
String sql_insert = "INSERT INTO mytable(content) VALUES (?)"; String sql_currval = "SELECT seq_mytable.CURRVAL FROM dual"; Connection connection = null; PreparedStatement statement = null; Statement currvalStatement = null; ResultSet currvalResultSet = null; try { connection = database.getConnection(); connection.setAutoCommit(false); statement = connection.prepareStatement(sql_insert); statement.setString(1, "test"); statement.executeUpdate(); currvalStatement = connection.createStatement(); currvalResultSet = currvalStatement.executeQuery(sql_currval); if (currvalResultSet.next()) { int id = currvalResultSet.getInt(1); } connection.commit(); // ...
private static void initTableValues() throws Exception { Properties props = new Properties(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(getUrl(), props); conn.setAutoCommit(false); try { createTestTable(getUrl(), "create table testVarcharKey " + " (key_string varchar not null primary key, kv integer) SALT_BUCKETS=4\n"); String query = "UPSERT INTO testVarcharKey VALUES(?,?)"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, "a"); stmt.setInt(2, 1); stmt.execute(); stmt.setString(1, "ab"); stmt.setInt(2, 2); stmt.execute(); stmt.setString(1, "abc"); stmt.setInt(2, 3); stmt.execute(); conn.commit(); } finally { conn.close(); } }
conn.setAutoCommit(false); selectPST = conn.prepareStatement(SELECT_UNDO_LOG_SQL); selectPST.setLong(1, branchId); selectPST.setString(2, xid); rs = selectPST.executeQuery(); while (rs.next()) { Blob b = rs.getBlob("rollback_info"); String rollbackInfo = StringUtils.blob2string(b); conn.commit(); conn.close();
private static void initPtsdbTableValues(long ts) throws Exception { ensureTableCreated(getUrl(),PTSDB_NAME,null, ts-2); String url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts; // Insert at timestamp 0 Properties props = new Properties(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(url, props); conn.setAutoCommit(true); PreparedStatement stmt = conn.prepareStatement("upsert into PTSDB VALUES ('x', 'y', ?, 0.5)"); stmt.setDate(1, D1); stmt.execute(); conn.close(); }
/** * Delete all testsuite-suite data from the database * * @throws SQLException */ public void deleteTestData() throws SQLException { Connection cn = null; try { cn = getDataSource().getConnection(); cn.setAutoCommit( false ); PreparedStatement pmt = cn.prepareStatement( "delete from GEOMTEST" ); if ( !pmt.execute() ) { int updateCount = pmt.getUpdateCount(); LOG.info( "Removing " + updateCount + " rows." ); } cn.commit(); pmt.close(); } finally { try { if ( cn != null ) { cn.close(); } } catch (SQLException e) { // nothing to do } } }
@Override public boolean put(List<TransactionRecord> trs) { PreparedStatement statement = null; try { this.connection.setAutoCommit(false); statement = this.connection.prepareStatement("insert into t_transaction values (?, ?)"); for (TransactionRecord tr : trs) { statement.setLong(1, tr.getOffset()); statement.setString(2, tr.getProducerGroup()); statement.addBatch(); } int[] executeBatch = statement.executeBatch(); this.connection.commit(); this.totalRecordsValue.addAndGet(updatedRows(executeBatch)); return true; } catch (Exception e) { log.warn("createDB Exception", e); return false; } finally { if (null != statement) { try { statement.close(); } catch (SQLException e) { log.warn("Close statement exception", e); } } } }
insertSql.delete(len - 1, len).append(")"); try (Connection connTarget = targetDS.getConnection(); PreparedStatement pstmt = connTarget.prepareStatement(insertSql.toString())) { connTarget.setAutoCommit(false); while (rs.next()) { pstmt.clearParameters(); + " WHERE "); appendCondition(dbMapping, deleteSql, values, rs); try (PreparedStatement pstmt2 = connTarget.prepareStatement(deleteSql.toString())) { int k = 1; for (Object val : values.values()) { connTarget.commit(); completed = true; connTarget.commit();
try (java.sql.Connection con = createConnection()) { con.setAutoCommit(false); try (Statement stm = con.createStatement()) { stm.execute(someQuery); // causes SQLException } catch(SQLException ex) { con.rollback(); con.setAutoCommit(true); throw ex; } con.commit(); con.setAutoCommit(true); }
public void testPublish() throws IOException, SQLException { publisher.publish(workUnitStates); InOrder inOrder = inOrder(conn, commands, workUnitState); inOrder.verify(conn, times(1)).setAutoCommit(false); inOrder.verify(commands, times(1)).copyTable(database, stagingTable, destinationTable); inOrder.verify(workUnitState, times(1)).setWorkingState(WorkUnitState.WorkingState.COMMITTED); inOrder.verify(conn, times(1)).commit(); inOrder.verify(conn, times(1)).close(); verify(commands, never()).deleteAll(database, destinationTable); }
try (Connection c = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1")) { c.setAutoCommit(false); try (Statement s = c.createStatement()) { s.execute("DELETE FROM \"Person\".PERSON WHERE _key = " + idx); try (Statement s = c.createStatement()) { s.execute("UPDATE \"Person\".PERSON SET lname = concat(lname, 'Updated')" + "WHERE _key = " + idx); try (Statement s = c.createStatement()) { s.execute("COMMIT");