List of usage examples for java.sql Connection setAutoCommit
void setAutoCommit(boolean autoCommit) throws SQLException;
From source file:com.fileanalyzer.dao.impl.FileStatisticDAOImpl.java
@Override public Long getMaxId() { Connection con = null; PreparedStatement statement = null; String sql = "select max(id) from " + FileStatistic.FileStatisticKey.TABLE; Long maxId = null;//from w ww. ja v a 2 s . c o m try { con = DBConnector.getConnection(); con.setAutoCommit(false); statement = con.prepareStatement(sql); ResultSet rs = statement.executeQuery(); if (rs.next()) maxId = rs.getLong(1); con.commit(); } catch (SQLException e) { handleException(e, con); } finally { doFinal(con, statement); } return maxId; }
From source file:localdomain.localhost.CasInitializer.java
@ManagedOperation public void createUser(@NotNull String username, @NotNull String clearTextPassword) { Connection cnn = null; try {/*from w w w.j a va2 s .c o m*/ cnn = dataSource.getConnection(); cnn.setAutoCommit(false); insertUser(username, clearTextPassword, cnn); cnn.commit(); } catch (RuntimeException e) { rollbackQuietly(cnn); String msg = "Exception creating '" + username + "': " + e; logger.warn(msg, e); throw new RuntimeException(msg); } catch (SQLException e) { rollbackQuietly(cnn); String msg = "Exception creating '" + username + "': " + e; logger.warn(msg, e); throw new RuntimeException(msg); } finally { closeQuietly(cnn); } }
From source file:com.mnt.base.das.DBFactory.java
public void beginTransaction() { TransactionOwner to = toHolder.get(); if (to == null) { Connection conn = retrieveConnection(); try {//from ww w. ja v a 2s . c o m conn.setAutoCommit(false); } catch (SQLException e) { log.error("fail to set connection auto commit as false.", e); throw new RuntimeException("Begin transaction error: fail to set connection auto commit as false.", e); } to = new TransactionOwner(conn); toHolder.set(to); } else { to.increaseDeep(); } }
From source file:com.gmt2001.SqliteStore.java
private static Connection CreateConnection(String dbname, int cache_size, boolean safe_write, boolean journal, boolean autocommit) { Connection connection = null; try {/*from www.ja v a 2s . c om*/ SQLiteConfig config = new SQLiteConfig(); config.setCacheSize(cache_size); config.setSynchronous( safe_write ? SQLiteConfig.SynchronousMode.FULL : SQLiteConfig.SynchronousMode.NORMAL); config.setTempStore(SQLiteConfig.TempStore.MEMORY); config.setJournalMode(journal ? SQLiteConfig.JournalMode.TRUNCATE : SQLiteConfig.JournalMode.OFF); connection = DriverManager.getConnection("jdbc:sqlite:" + dbname.replaceAll("\\\\", "/"), config.toProperties()); connection.setAutoCommit(autocommit); } catch (SQLException ex) { com.gmt2001.Console.err.printStackTrace(ex); } return connection; }
From source file:com.redhat.victims.database.VictimsSqlDB.java
public void synchronize() throws VictimsException { Throwable throwable = null;//from w w w . j a v a 2 s. c o m try { Connection connection = getConnection(); connection.setAutoCommit(false); Savepoint savepoint = connection.setSavepoint(); try { VictimsService service = new VictimsService(); Date since = lastUpdated(); int removed = remove(connection, service.removed(since)); int updated = update(connection, service.updates(since)); if (removed > 0 || updated > 0) { cache.purge(); } setLastUpdate(new Date()); } catch (IOException e) { throwable = e; } catch (SQLException e) { throwable = e; } finally { if (throwable != null) { connection.rollback(savepoint); } connection.releaseSavepoint(savepoint); connection.commit(); connection.close(); } } catch (SQLException e) { throwable = e; } if (throwable != null) { throw new VictimsException("Failed to sync database", throwable); } }
From source file:com.sonicle.webtop.core.app.ConnectionManager.java
/** * Returns a connection from desired namespace. * @param namespace The pool namespace./*from ww w . j ava 2s . c o m*/ * @param dataSourceName The dataSource name. * @param autoCommit False to disable auto-commit mode; defaults to True. * @return A ready Connection object. * @throws SQLException */ public Connection getConnection(String namespace, String dataSourceName, boolean autoCommit) throws SQLException { Check.notNull(namespace); Check.notNull(dataSourceName); String poolName = poolName(namespace, dataSourceName); Connection con = getPool(poolName).getConnection(); con.setAutoCommit(autoCommit); return con; }
From source file:com.fileanalyzer.dao.impl.FilesDAOImpl.java
@Override public Long getMaxId() { Connection con = null; PreparedStatement statement = null; String sql = "select max(id) from " + Files.FilesFieldsKey.TABLE; Long maxId = null;// w w w .jav a2s . c om try { con = DBConnector.getConnection(); con.setAutoCommit(false); statement = con.prepareStatement(sql); ResultSet rs = statement.executeQuery(); if (rs.next()) maxId = rs.getLong(1); con.commit(); } catch (SQLException e) { handleException(e, con); } finally { doFinal(con, statement); } return maxId; }
From source file:org.gsoft.admin.ScriptRunner.java
/** * Runs an SQL script (read in using the Reader parameter) * // w ww . j a v a 2s .co m * @param reader * - the source of the script */ public void runScript(Reader reader) throws IOException, SQLException { try { Connection connection = this.dataSource.getConnection(); boolean originalAutoCommit = connection.getAutoCommit(); try { if (originalAutoCommit != this.autoCommit) { connection.setAutoCommit(this.autoCommit); } runScript(connection, reader); } finally { connection.setAutoCommit(originalAutoCommit); } } catch (IOException e) { throw e; } catch (SQLException e) { throw e; } catch (Exception e) { throw new RuntimeException("Error running script. Cause: " + e, e); } }
From source file:chh.utils.db.source.common.JdbcClient.java
public void executeInsertQuery(String query, List<List<Column>> columnLists) { Connection connection = null; try {/* w w w . j a va 2 s. c o m*/ connection = connectionProvider.getConnection(); boolean autoCommit = connection.getAutoCommit(); if (autoCommit) { connection.setAutoCommit(false); } LOG.debug("Executing query {}", query); PreparedStatement preparedStatement = connection.prepareStatement(query); if (queryTimeoutSecs > 0) { preparedStatement.setQueryTimeout(queryTimeoutSecs); } for (List<Column> columnList : columnLists) { setPreparedStatementParams(preparedStatement, columnList); preparedStatement.addBatch(); } int[] results = preparedStatement.executeBatch(); if (Arrays.asList(results).contains(Statement.EXECUTE_FAILED)) { connection.rollback(); throw new RuntimeException( "failed at least one sql statement in the batch, operation rolled back."); } else { try { connection.commit(); } catch (SQLException e) { throw new RuntimeException("Failed to commit insert query " + query, e); } } } catch (SQLException e) { throw new RuntimeException("Failed to execute insert query " + query, e); } finally { closeConnection(connection); } }
From source file:com.cloudera.sqoop.manager.PostgresqlTest.java
@Before public void setUp() { super.setUp(); LOG.debug("Setting up another postgresql test: " + CONNECT_STRING); SqoopOptions options = new SqoopOptions(CONNECT_STRING, TABLE_NAME); options.setUsername(DATABASE_USER);//ww w . j a va 2s .co m ConnManager manager = null; Connection connection = null; Statement st = null; try { manager = new PostgresqlManager(options); connection = manager.getConnection(); connection.setAutoCommit(false); st = connection.createStatement(); // create the database table and populate it with data. try { // Try to remove the table first. DROP TABLE IF EXISTS didn't // get added until pg 8.3, so we just use "DROP TABLE" and ignore // any exception here if one occurs. st.executeUpdate("DROP TABLE " + TABLE_NAME); } catch (SQLException e) { LOG.info("Couldn't drop table " + TABLE_NAME + " (ok)"); LOG.info(e.toString()); // Now we need to reset the transaction. connection.rollback(); } st.executeUpdate("CREATE TABLE " + TABLE_NAME + " (" + "id INT NOT NULL PRIMARY KEY, " + "name VARCHAR(24) NOT NULL, " + "start_date DATE, " + "salary FLOAT, " + "dept VARCHAR(32))"); st.executeUpdate( "INSERT INTO " + TABLE_NAME + " VALUES(" + "1,'Aaron','2009-05-14',1000000.00,'engineering')"); st.executeUpdate("INSERT INTO " + TABLE_NAME + " VALUES(" + "2,'Bob','2009-04-20',400.00,'sales')"); st.executeUpdate("INSERT INTO " + TABLE_NAME + " VALUES(" + "3,'Fred','2009-01-23',15.00,'marketing')"); connection.commit(); } catch (SQLException sqlE) { LOG.error("Encountered SQL Exception: " + sqlE); sqlE.printStackTrace(); fail("SQLException when running test setUp(): " + sqlE); } finally { try { if (null != st) { st.close(); } if (null != manager) { manager.close(); } } catch (SQLException sqlE) { LOG.warn("Got SQLException when closing connection: " + sqlE); } } LOG.debug("setUp complete."); }