List of usage examples for java.sql Connection rollback
void rollback() throws SQLException;
Connection
object. From source file:com.quartzdesk.executor.common.spring.db.DataSourceScriptExecutorFactoryBean.java
/** * Applies the specified SQL init script to the database. * * @param con a JDBC connection.//from w w w. ja v a2 s. c o m * @param initScript the SQL init script to apply. */ private void applyInitScript(Connection con, Resource initScript) { URL initScriptUrl; try { initScriptUrl = initScript.getURL(); } catch (IOException e) { throw new RuntimeException("Error obtaining URL of SQL script resource: " + initScript); } boolean autoCommit = true; try { autoCommit = con.getAutoCommit(); DatabaseScriptExecutor scriptExecutor = new DatabaseScriptExecutor(); scriptExecutor.addScriptUrl(initScriptUrl); scriptExecutor.executeScripts(con); // if auto-commit disabled, we need to commit the changes if (!autoCommit) con.commit(); log.info("Successfully applied SQL script: {} to database.", initScriptUrl); } catch (SQLException e) { try { if (!autoCommit) con.rollback(); } catch (SQLException se) { log.warn("Error rolling-back connection: " + con, se); } throw new RuntimeException("Error applying SQL script: " + initScriptUrl, e); } }
From source file:com.bloidonia.vertx.mods.JdbcProcessor.java
/**************************************************************************** **/*ww w. j a va 2 s . c om*/ ** Transaction handling ** ****************************************************************************/ private void doTransaction(Message<JsonObject> message) { Connection connection = null; try { connection = poolMap.get(address).getConnection(); connection.setAutoCommit(false); doTransaction(message, connection); } catch (SQLException ex) { sendError(message, "Caught exception in TRANSACTION. Rolling back...", ex); try { connection.rollback(); } catch (SQLException exx) { logger.error("Failed to rollback", exx); } SilentCloser.close(connection); } }
From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java
protected int executeUpdateForKey(final String query, final String... keyValues) { int result = 0; try {/*from www . jav a2s . c o m*/ final Connection conn = poolingDataSource.getConnection(); try { result = new QueryRunner().update(conn, query, (Object[]) keyValues); conn.commit(); } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } catch (SQLException e) { log.error("Sql exception", e); throw new UserMetadataServiceException( String.format("Failed to save data for %s", Arrays.toString(keyValues)), e); } return result; }
From source file:dao.PendingfriendDaoDb.java
/** * adds the guestLogin as a friend/*from w w w . j a v a2s . c om*/ * @param guestLogin - the guestId * @param loginId - the loginid * @param login - the login * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void addFriend(String guestLogin, String loginId, String login) throws BaseDaoException { if (RegexStrUtil.isNull(guestLogin) || RegexStrUtil.isNull(loginId)) { throw new BaseDaoException("params are null"); } Hdlogin hdlogin = getLoginid(guestLogin); if (hdlogin == null) { throw new BaseDaoException("hdlogin is null for guestLogin " + guestLogin); } String guestId = hdlogin.getValue(DbConstants.LOGIN_ID); if (RegexStrUtil.isNull(guestId)) { throw new BaseDaoException("guestId is null for guestLogin " + guestId); } /** * Get scalability datasource for hdfriends - not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, getParentInfo() " + sourceName); } /** * add friend */ Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); if (conn != null) { friendAddQuery.run(conn, guestId, loginId); } } catch (Exception e) { try { if (conn != null) { conn.rollback(); } } catch (Exception e1) { // (Exception e1) try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { // (Exception e2) throw new BaseDaoException("conn.setAutoCommit() error in friendAddQuery() guestId = " + guestId + ", loginId = " + loginId, e2); } throw new BaseDaoException( "conn.rollback error in friendAddQuery() guestId = " + guestId + ", loginId = " + loginId, e1); } throw new BaseDaoException( "error occured while executing friendAddQuery() guestId =" + guestId + ", loginId = " + loginId, e); } // (Exception e) /** * commit the transaction */ try { if (conn != null) { conn.commit(); conn.setAutoCommit(true); conn.close(); } } catch (Exception e) { throw new BaseDaoException("con.commit()/conn.setAutoCommit()/conn.close() exception", e); } /** * delete the cache based on the loginId */ Fqn fqn = cacheUtil.fqn(DbConstants.OUT_PENDING); if (treeCache.exists(fqn, guestId)) { treeCache.remove(fqn, guestId); } fqn = cacheUtil.fqn(DbConstants.IN_PENDING); if (treeCache.exists(fqn, loginId)) { treeCache.remove(fqn, loginId); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, guestLogin)) { treeCache.remove(fqn, guestLogin); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, login)) { treeCache.remove(fqn, login); } }
From source file:com.viettel.logistic.wms.service.StockTransServiceImpl.java
private void rollback(Session session, Transaction transaction, Connection con) { try {// w ww . j ava2 s. co m if (con != null) { con.rollback(); } if (transaction != null) { transaction.rollback(); } if (con != null && !con.isClosed()) { con.close(); } } catch (SQLException ex) { Logger.getLogger(StockImportServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } if (session != null && session.isOpen()) { session.close(); } }
From source file:com.healthcit.cacure.utils.DBSchemaUpdater.java
@Override public void afterPropertiesSet() throws Exception { Connection connection = DataSourceUtils.getConnection(dataSource); connection.setAutoCommit(false);/*from w ww.j a va2s .com*/ try { Statement statement = connection.createStatement(); try { long version = 0; try { ResultSet rs = statement.executeQuery("select schema_version from sys_variables limit 1;"); try { if (!rs.next()) { throw new RuntimeException("Seems there is no any row in sys_variables table."); } version = rs.getLong(1); } finally { rs.close(); } } catch (PSQLException e) { // it's needed for executing more scripts successfully connection.rollback(); log.info("Can't find sys_variables tables. Appling initial script."); String initialScriptStatements = getStatementsFor(0); if (initialScriptStatements == null) { throw new RuntimeException("Can't find initial script."); } statement.executeUpdate(initialScriptStatements); //there is already schema_version at 0 connection.commit(); log.info("Initial script succesfully executed."); } for (long v = version + 1;; v++) { String statements = getStatementsFor(v); if (statements == null) { break; } log.info("Updating schema to " + v + " version..."); statement.execute(statements); statement.executeUpdate("update sys_variables set schema_version = " + v + ";"); connection.commit(); log.info("OK"); } } catch (BatchUpdateException e) { if (e.getNextException() != null) { e.getNextException().printStackTrace(); } e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); connection.rollback(); } finally { statement.close(); } } finally { DataSourceUtils.releaseConnection(connection, dataSource); } }
From source file:com.c9.vertx.mods.JdbcProcessor.java
/**************************************************************************** ** /* w w w .j av a 2 s . co m*/ ** Transaction handling ** ****************************************************************************/ private void doTransaction(Message<JsonObject> message) { Connection connection = null; try { connection = getConnection(address); connection.setAutoCommit(false); doTransaction(message, connection); } catch (SQLException ex) { sendError(message, "Caught exception in TRANSACTION. Rolling back...", ex); try { connection.rollback(); } catch (SQLException exx) { logger.error("Failed to rollback", exx); } SilentCloser.close(connection); } }
From source file:com.che.software.testato.domain.dao.jdbc.impl.PrioritizationDAO.java
/** * Creates the prioritization related to the given hierarchy. * // w ww. j a v a 2 s. c o m * @author Clement HELIOU (clement.heliou@che-software.com). * @param hierarchyId the given hierarchy id. * @since July, 2011. * @throws PrioritizationCreationDAOException if an error occurs during the * creation. */ @Override public void createHierarchyPrioritization(int hierarchyId) throws PrioritizationCreationDAOException { LOGGER.debug("createHierarchyPrioritization(" + hierarchyId + ")."); Connection connection = null; try { connection = getDataSource().getConnection(); connection.setAutoCommit(false); getQueryRunner().update(connection, "INSERT INTO prioritization(prioritization_id) VALUES(nextval('prioritization_seq')) "); Integer createdPrioritization = (Integer) getQueryRunner().query(connection, "SELECT MAX(prioritization_id)::int AS prioritizationId FROM prioritization ", new ScalarHandler("prioritizationId")); getQueryRunner().update(connection, "INSERT INTO hierarchy_prioritization(hierarchy_id, prioritization_id) VALUES(?,?) ", new Object[] { hierarchyId, createdPrioritization }); connection.commit(); } catch (SQLException e) { try { connection.rollback(); } catch (SQLException e1) { throw new PrioritizationCreationDAOException(e); } throw new PrioritizationCreationDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.che.software.testato.domain.dao.jdbc.impl.ItemDAO.java
/** * Update an item to refine it./*from ww w . j a va 2s . c o m*/ * * @author Clement HELIOU (clement.heliou@che-software.com). * @param itemId the item id. * @return the created action plan id. * @since July, 2011. * @throws ItemUpdateDAOException if an error occurs during the update. */ @Override public int refineItem(int itemId) throws ItemUpdateDAOException { LOGGER.debug("refineItem(" + itemId + ")."); Connection connection = null; try { connection = getDataSource().getConnection(); connection.setAutoCommit(false); getQueryRunner().update(connection, "INSERT INTO action_plan(action_plan_id) VALUES(nextval('action_plan_seq')) "); Integer createdActionPlan = (Integer) getQueryRunner().query(connection, "SELECT MAX(action_plan_id)::int AS actionPlanId FROM action_plan ", new ScalarHandler("actionPlanId")); getQueryRunner().update(connection, "UPDATE item SET action_plan_id = ? WHERE item_id = ? ", new Object[] { createdActionPlan, itemId }); connection.commit(); return createdActionPlan; } catch (SQLException e) { try { connection.rollback(); } catch (SQLException e1) { throw new ItemUpdateDAOException(e1); } throw new ItemUpdateDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.netflix.metacat.usermetadata.mysql.MySqlLookupService.java
/** * Saves the lookup value.//from ww w .j a va 2 s. co m * @param name lookup name * @param values multiple values * @return returns the lookup with the given name. */ @Override public Lookup addValues(final String name, final Set<String> values) { Lookup lookup = null; try { final Connection conn = getDataSource().getConnection(); try { lookup = findOrCreateLookupByName(name, conn); Set<String> inserts = Sets.newHashSet(); final Set<String> lookupValues = lookup.getValues(); if (lookupValues == null || lookupValues.isEmpty()) { inserts = values; lookup.setValues(values); } else { inserts = Sets.difference(values, lookupValues); } if (!inserts.isEmpty()) { insertLookupValues(lookup.getId(), inserts, conn); } conn.commit(); } catch (SQLException e) { conn.rollback(); throw e; } finally { conn.close(); } } catch (SQLException e) { final String message = String.format("Failed to set the lookup values for name %s", name); log.error(message, e); throw new UserMetadataServiceException(message, e); } return lookup; }