List of usage examples for java.sql Connection rollback
void rollback() throws SQLException;
Connection
object. From source file:org.apache.hadoop.hive.metastore.txn.TxnDbUtil.java
private static void closeResources(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try {//from w w w . j a v a 2 s .c om rs.close(); } catch (SQLException e) { System.err.println("Error closing ResultSet: " + e.getMessage()); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { System.err.println("Error closing Statement: " + e.getMessage()); } } if (conn != null) { try { conn.rollback(); } catch (SQLException e) { System.err.println("Error rolling back: " + e.getMessage()); } try { conn.close(); } catch (SQLException e) { System.err.println("Error closing Connection: " + e.getMessage()); } } }
From source file:com.clustercontrol.commons.util.JdbcBatchExecutor.java
/** * ?/*from w w w . j a v a 2s .c o m*/ * * @param query * insert???update */ public static void execute(List<JdbcBatchQuery> queryList) { Connection conn = null; long start = HinemosTime.currentTimeMillis(); JpaTransactionManager tm = null; try { tm = new JpaTransactionManager(); conn = tm.getEntityManager().unwrap(java.sql.Connection.class); conn.setAutoCommit(false); for (JdbcBatchQuery query : queryList) try (PreparedStatement pstmt = conn.prepareStatement(query.getSql())) { query.addBatch(pstmt); pstmt.executeBatch(); } if (!tm.isNestedEm()) { conn.commit(); } } catch (Exception e) { log.warn(e); if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { log.warn(e1); } } } finally { if (tm != null) { tm.close(); } } long time = HinemosTime.currentTimeMillis() - start; String className = ""; if (queryList.size() != 0) { className = queryList.get(0).getClass().getSimpleName(); } String message = String.format("Execute [%s] batch: %dms. size=%d", className, time, queryList.size()); if (time > 3000) { log.warn(message); } else if (time > 1000) { log.info(message); } else { log.debug(message); } }
From source file:dbutils.ExampleJDBC.java
/** * ?//from w w w. j a va 2 s . co m */ public static void insertAndUpdateData() throws SQLException { Connection conn = getConnection(); QueryRunner qr = new QueryRunner(); try { //??insert? Object[] insertParams = { "John Doe", "", 12, 3 }; int inserts = qr.update(conn, "INSERT INTO test_student(name,gender,age,team_id) VALUES (?,?,?,?)", insertParams); System.out.println("inserted " + inserts + " data"); Object[] updateParams = { "John Doe Update", "John Doe" }; int updates = qr.update(conn, "UPDATE test_student SET name=? WHERE name=?", updateParams); System.out.println("updated " + updates + " data"); } catch (SQLException e) { e.printStackTrace(); conn.rollback(); } finally { DbUtils.close(conn); } }
From source file:com.stratelia.webactiv.util.DBUtil.java
public static void rollback(Connection connection) { if (connection != null) { try {//from w w w.j a v a 2s.c o m if (!connection.getAutoCommit() && !connection.isClosed()) { connection.rollback(); } } catch (SQLException e) { SilverTrace.error("util", "DBUtil.close", "util.CAN_T_ROLLBACK_CONNECTION", e); } } }
From source file:com.trackplus.ddl.DataWriter.java
private static void executeUpdate(Connection con, Statement stmt, String s) throws DDLException { try {//www .j av a2 s . com stmt.executeUpdate(s); } catch (SQLException e) { LOGGER.error("Error execute script line:" + e.getMessage()); LOGGER.warn("-------------\n\n"); LOGGER.warn(s); LOGGER.warn("-------------\n\n"); if (LOGGER.isDebugEnabled()) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } try { stmt.close(); con.rollback(); con.setAutoCommit(true); } catch (SQLException ex) { throw new DDLException(e.getMessage(), e); } throw new DDLException(e.getMessage(), e); } }
From source file:com.laudandjolynn.mytv.Main.java
/** * ???//from ww w . j a v a2 s .c o m */ private static void initDbData0(MyTvData data) { if (data.isDataInited()) { logger.info("init data had insert into db."); return; } Properties tvStationProp = new Properties(); try { tvStationProp.load(Main.class.getResourceAsStream("/" + Constant.TV_STATION_INIT_DATA_FILE_NAME)); } catch (IOException e) { throw new MyTvException( "error occur while load property file: " + Constant.TV_STATION_INIT_DATA_FILE_NAME, e); } Collection<Object> values = tvStationProp.values(); List<String> insertSqlList = new ArrayList<String>(values.size()); String insertSql = "insert into my_tv (stationName,displayName,classify,channel,sequence)"; for (Object value : values) { insertSqlList.add(insertSql + " values (" + value.toString() + ")"); } Connection conn = null; Statement stmt = null; try { conn = DataSourceManager.getConnection(); conn.setAutoCommit(false); stmt = conn.createStatement(); for (String sql : insertSqlList) { stmt.addBatch(sql); logger.info("execute sql: " + sql); } stmt.executeBatch(); conn.commit(); data.writeData(null, Constant.XML_TAG_DATA, "true"); } catch (SQLException e) { if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { throw new MyTvException("error occur while rollback transaction.", e1); } } throw new MyTvException("error occur while execute sql on db.", e); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { throw new MyTvException("error occur while close statement.", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { throw new MyTvException("error occur while close sqlite connection.", e); } } } }
From source file:gridool.replication.ReplicationManager.java
private static boolean prepareReplicaTable(@Nonnull final Connection conn, @Nonnull final String replicaTableName, final boolean autoCommit) { final String ddl = "CREATE TABLE \"" + replicaTableName + "\"(dbname varchar(30) PRIMARY KEY, nodeinfo VARCHAR(30), entryno INT auto_increment)"; try {/*from w w w . j a va 2 s . c o m*/ JDBCUtils.update(conn, ddl); if (!autoCommit) { conn.commit(); } } catch (SQLException e) { if (LOG.isDebugEnabled()) { LOG.debug("failed executing a query: " + ddl, e); } // avoid table already exists error if (!autoCommit) { try { conn.rollback(); } catch (SQLException sqle) { LOG.warn("failed to rollback", sqle); } } return false; } return true; }
From source file:gridool.util.jdbc.JDBCUtils.java
public static int[] batch(Connection conn, String... sqls) throws SQLException { final boolean autoCommit = conn.getAutoCommit(); if (autoCommit) { conn.setAutoCommit(false);/*from w ww. java2 s .c om*/ } Statement stmt = null; int[] rows = null; try { stmt = conn.createStatement(); for (int i = 0; i < sqls.length; i++) { verboseQuery(sqls[i], (Object[]) null); stmt.addBatch(sqls[i]); } rows = stmt.executeBatch(); conn.commit(); } catch (SQLException e) { conn.rollback(); rethrow(e, sqls); } finally { close(stmt); } // change back commit mode. if (autoCommit) { conn.setAutoCommit(true); } return rows; }
From source file:com.laudandjolynn.mytv.Main.java
/** * ??//from www. j a v a 2 s. co m */ private static void initDb(MyTvData data) { if (data.isDbInited()) { logger.debug("db have already init."); return; } File myTvDataFilePath = new File(Constant.MY_TV_DATA_FILE_PATH); Connection conn = null; Statement stmt = null; try { conn = DataSourceManager.getConnection(); conn.setAutoCommit(false); stmt = conn.createStatement(); List<String> sqlList = loadSql(); for (String sql : sqlList) { stmt.addBatch(sql); logger.info("execute sql: " + sql); } stmt.executeBatch(); conn.commit(); DataSourceManager.DATA_SOURCE_PROP.remove(DataSourceManager.RES_KEY_DB_SQL_LIST); data.writeData(null, Constant.XML_TAG_DB, "true"); } catch (SQLException e) { if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { throw new MyTvException("error occur while rollback transaction.", e); } } myTvDataFilePath.deleteOnExit(); throw new MyTvException("error occur while execute sql on db.", e); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { throw new MyTvException("error occur while close statement.", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { throw new MyTvException("error occur while close sqlite connection.", e); } } } }
From source file:org.alinous.plugin.derby.DerbyConnectionFactory.java
@Override public void passivateObject(Object arg0) throws Exception { Connection con = (Connection) arg0; con.rollback(); super.passivateObject(arg0); }