private static void execute(Connection connection, String sql) throws SQLException { try (Statement stmt = connection.createStatement()) { stmt.execute(sql); connection.commit(); } }
private static void deleteManualRules(Connection connection) throws SQLException { try (PreparedStatement statement = connection.prepareStatement("DELETE FROM rules WHERE rules.plugin_name='manual'")) { statement.execute(); // commit is useless on some databases connection.commit(); } }
/** * Default organization must never be deleted */ private static void truncateOrganizations(String tableName, Statement ddlStatement, Connection connection) throws SQLException { try (PreparedStatement preparedStatement = connection.prepareStatement("delete from organizations where kee <> ?")) { preparedStatement.setString(1, "default-organization"); preparedStatement.execute(); // commit is useless on some databases connection.commit(); } }
@Test public void testUpperCaseEncodingType() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String tableName = generateUniqueName(); String ddl = "CREATE TABLE " + tableName + " ( pk VARCHAR(10) NOT NULL CONSTRAINT PK PRIMARY KEY (pk))"; conn.createStatement().execute(ddl); PreparedStatement ps = conn.prepareStatement("UPSERT INTO " + tableName + " (pk) VALUES (?)"); ps.setString(1, "1"); ps.execute(); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM " + tableName + " WHERE pk = ENCODE(1, 'BASE62')"); assertTrue(rs.next()); }
public int getDBVersion() throws Exception { int dbVersion; try (Connection connection = ds.getConnection(); Statement statement = connection.createStatement()) { try (ResultSet rs = statement.executeQuery("SELECT current_setting('server_version_num')")) { rs.next(); dbVersion = rs.getInt(1); connection.commit(); } } return dbVersion; }
@Test public void testTransactionWithEnforceReadOnly() throws Exception { tm.setEnforceReadOnly(true); given(con.getAutoCommit()).willReturn(true); Statement stmt = mock(Statement.class); given(con.createStatement()).willReturn(stmt); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); tt.setReadOnly(true); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); // something transactional } }); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con, stmt); ordered.verify(con).setAutoCommit(false); ordered.verify(stmt).executeUpdate("SET TRANSACTION READ ONLY"); ordered.verify(stmt).close(); ordered.verify(con).commit(); ordered.verify(con).setAutoCommit(true); ordered.verify(con).close(); }
@SuppressFBWarnings("DMI_EMPTY_DB_PASSWORD") public void init() throws Exception { Class.forName("org.h2.Driver"); connection = DriverManager.getConnection("jdbc:h2:~/ehcache-demo-peeper", "sa", ""); try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE IF NOT EXISTS PEEPS (" + "id bigint auto_increment primary key," + "PEEP_TEXT VARCHAR(142) NOT NULL" + ")"); connection.commit(); } }
@Test public void testSplit_Upsert() throws SQLException { Connection conn = DriverManager.getConnection(getUrl()); initTable(conn, "ONE,TWO,THREE"); conn.createStatement().executeUpdate( "UPSERT INTO " + tableName + " (ID, ARR) SELECT ID, " + "REGEXP_SPLIT(VAL, ',') FROM " + tableName); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT ARR FROM " + tableName); assertTrue(rs.next()); Array array = rs.getArray(1); String[] values = (String[]) array.getArray(); assertArrayEquals(new String[]{ "ONE", "TWO", "THREE" }, values); }
public String selectClonedProjectByToken(String token) throws Exception { try (Connection connection = ds.getConnection(); PreparedStatement statement = connection.prepareStatement(selectClonedProjectByToken)) { statement.setString(1, token); try (ResultSet rs = statement.executeQuery()) { connection.commit(); if (rs.next()) { return rs.getString("json"); } } } return null; }
try { connection = dataSource.getConnection(); stmt = connection.createStatement(); if (stmt.execute(query)) { ResultSet rset = stmt.getResultSet(); int count = 0; while (rset.next()) { count++; LOGGER.info("QUERY(" + query + ") Update count: " + updateCount); connection.commit(); } catch (SQLException ex) { try {
/** * @param batched Batch mode flag. * @throws SQLException if failed. */ private void doTestAutoCommit(boolean batched) throws SQLException { IgniteCache<Integer, ?> cache = grid(0).cache("ints"); try (Connection c = c(false, NestedTxMode.ERROR)) { try (Statement s = c.createStatement()) { assertFalse(s.executeQuery("SELECT * from INTS").next()); if (batched) { s.addBatch("INSERT INTO INTS(k, v) values(1, 1)"); s.executeBatch(); } else s.execute("INSERT INTO INTS(k, v) values(1, 1)"); // We haven't committed anything yet - this check shows that autoCommit flag is in effect. assertTrue(cache.query(new SqlFieldsQuery("SELECT * from INTS")).getAll().isEmpty()); // We should see own updates. assertTrue(s.executeQuery("SELECT * from INTS").next()); c.commit(); c.setAutoCommit(true); assertEquals(1, cache.get(1)); assertTrue(s.executeQuery("SELECT * from INTS").next()); } } }
given(con.prepareStatement("some SQL statement")).willReturn(ps); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); if (timeout > 1) { verify(ps).setQueryTimeout(timeout - 1); verify(con).commit();
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { Connection conn = DriverManager.getConnection(DFLT_CONN_URL, "sa", ""); Statement stmt = conn.createStatement(); stmt.executeUpdate("DROP TABLE IF EXISTS Organization"); stmt.executeUpdate("DROP TABLE IF EXISTS Person"); stmt.executeUpdate("CREATE TABLE Organization (id integer PRIMARY KEY, name varchar(50), city varchar(50))"); stmt.executeUpdate("CREATE TABLE Person (id integer PRIMARY KEY, org_id integer, name varchar(50))"); conn.commit(); U.closeQuiet(stmt); U.closeQuiet(conn); startGrid(); }
@Test public void testSystemTableHasDoubleForExponentialNumber() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String test = generateUniqueName(); String ddl = "CREATE TABLE " + test + " (id VARCHAR not null primary key, num FLOAT)"; conn.createStatement().execute(ddl); String dml = "UPSERT INTO " + test + "(id,num) VALUES ('testid', 1.2E3)"; conn.createStatement().execute(dml); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT 1.2E3 FROM \"SYSTEM\".\"CATALOG\" LIMIT 1"); assertTrue(rs.next()); assertTrue(rs.getObject(1) instanceof Double); }
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(); // ...
@Test @Ignore("Wedges Derby") public void deadlockDetected() throws Exception { final TxnHandler tHndlr = (TxnHandler)txnHandler; Connection conn = tHndlr.getDbConn(Connection.TRANSACTION_SERIALIZABLE); Statement stmt = conn.createStatement(); long now = tHndlr.getDbTime(conn); stmt.executeUpdate("insert into TXNS (txn_id, txn_state, txn_started, txn_last_heartbeat, " + tHndlr.LOCK_WAITING + "', '" + tHndlr.LOCK_EXCLUSIVE + "', " + now + ", 'fred', " + "'scooby.com')"); conn.commit(); tHndlr.closeDbConn(conn);
@SuppressFBWarnings("DMI_EMPTY_DB_PASSWORD") public void init() throws Exception { dataCache.setupCache(); Class.forName("org.h2.Driver"); connection = DriverManager.getConnection("jdbc:h2:~/ehcache-demo-peeper", "sa", ""); try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE IF NOT EXISTS PEEPS (" + "id bigint auto_increment primary key," + "PEEP_TEXT VARCHAR(142) NOT NULL" + ")"); connection.commit(); } }
/** * Groups sonar-users is referenced by the default organization as its default group. */ private static void truncateGroups(String tableName, Statement ddlStatement, Connection connection) throws SQLException { try (PreparedStatement preparedStatement = connection.prepareStatement("delete from groups where name <> ?")) { preparedStatement.setString(1, "sonar-users"); preparedStatement.execute(); // commit is useless on some databases connection.commit(); } }
public String selectHostByToken(String token) { try (Connection connection = ds.getConnection(); PreparedStatement statement = connection.prepareStatement(selectHostByToken)) { statement.setString(1, token); try (ResultSet rs = statement.executeQuery()) { connection.commit(); if (rs.next()) { return rs.getString("host"); } } } catch (Exception e) { log.error("Error getting token host. Reason : {}", e.getMessage()); } return null; }
private void truncateAnalysisTables(Connection connection) throws SQLException { try (Statement statement = connection.createStatement()) { // Clear inspection tables for (String table : ANALYSIS_TABLES) { statement.execute(createTruncateSql(table.toLowerCase(Locale.ENGLISH))); // commit is useless on some databases connection.commit(); } // Clear resource related tables for (String table : RESOURCE_RELATED_TABLES) { statement.execute("DELETE FROM " + table + " WHERE resource_id IS NOT NULL"); connection.commit(); } } }