List of usage examples for org.apache.commons.dbutils DbUtils commitAndClose
public static void commitAndClose(Connection conn) throws SQLException
Connection
then closes it, avoid closing if null. From source file:io.apiman.gateway.engine.jdbc.JdbcRegistry.java
/** * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler) *//*from w w w . j a va2 s . com*/ @Override public void publishApi(Api api, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // First delete any record we might already have. run.update(conn, "DELETE FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion()); // Now insert a row for the api. String bean = mapper.writeValueAsString(api); run.update(conn, "INSERT INTO gw_apis (org_id, id, version, bean) VALUES (?, ?, ?, ?)", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null, Void.class)); } catch (SQLException | JsonProcessingException e) { handler.handle(AsyncResultImpl.create(e)); } }
From source file:dbutils.DbUtilsTemplate.java
/** * sql?,?????/*from w w w . j a v a 2s. co m*/ * * @param sql sql? * @param params ? * @return ?? */ public int update(String sql, Object[] params) throws SQLException { queryRunner = new QueryRunner(); int affectedRows = 0; Connection conn = null; try { conn = dataSource.getConnection(); if (params == null) { affectedRows = queryRunner.update(conn, sql); } else { affectedRows = queryRunner.update(conn, sql, params); } } catch (SQLException e) { LOG.error("Error occured while attempting to update data", e); if (conn != null) { conn.rollback(); } throw e; } finally { if (conn != null) DbUtils.commitAndClose(conn); } return affectedRows; }
From source file:gov.nih.nci.cacisweb.dao.virtuoso.VirtuosoCommonUtilityDAO.java
/** * This method provides a central location for managing commit and close operations that need to be performed on the * database connection at the end of the transaction. Typically intended to be called from the session bean. *///from w w w . jav a 2s.c o m public void commitAndCloseCaCISConnection() throws DAOException { try { DbUtils.commitAndClose(cacisConnection); DbUtils.closeQuietly(rs); DbUtils.closeQuietly(pstmt); DbUtils.closeQuietly(stmt); } catch (SQLException sqle) { log.error(sqle.getMessage()); throw new DAOException(sqle.getMessage()); } }
From source file:io.apiman.gateway.engine.jdbc.JdbcRegistry.java
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) *//* w ww . j av a2s . c om*/ @Override public void registerClient(Client client, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // Validate the client and populate the api map with apis found during validation. validateClient(client, conn); // Remove any old data first, then (re)insert run.update(conn, "DELETE FROM gw_clients WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ client.getOrganizationId(), client.getClientId(), client.getVersion()); String bean = mapper.writeValueAsString(client); run.update(conn, "INSERT INTO gw_clients (api_key, org_id, id, version, bean) VALUES (?, ?, ?, ?, ?)", //$NON-NLS-1$ client.getApiKey(), client.getOrganizationId(), client.getClientId(), client.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null)); } catch (Exception re) { DbUtils.rollbackAndCloseQuietly(conn); handler.handle(AsyncResultImpl.create(re, Void.class)); } }
From source file:io.apiman.gateway.engine.jdbc.PollCachingJdbcRegistry.java
/** * Stores a "dataversion" record in the ES store. There is only a single one of these. The * return value of the add will include the version number of the entity. This version * number is what we use to determine whether our cache is stale. *///from w w w .j a va 2s .c o m protected void updateDataVersion() { Connection conn = null; try { long newVersion = System.currentTimeMillis(); conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); run.update(conn, "DELETE FROM gw_dataversion"); //$NON-NLS-1$ run.update(conn, "INSERT INTO gw_dataversion (version) VALUES (?)", //$NON-NLS-1$ newVersion); DbUtils.commitAndClose(conn); dataVersion = newVersion; } catch (SQLException e) { dataVersion = -1; } }
From source file:dbutils.DbUtilsTemplate.java
/** * ?sql?//w w w . j a va 2 s . c o m * * @param sql sql? * @param params ? * @return ?? */ public int[] batchUpdate(String sql, Object[][] params) throws SQLException { queryRunner = new QueryRunner(); int[] affectedRows = new int[0]; Connection conn = null; try { conn = dataSource.getConnection(); affectedRows = queryRunner.batch(conn, sql, params); } catch (SQLException e) { LOG.error("Error occured while attempting to batch update data", e); if (conn != null) { conn.rollback(); } throw e; } finally { if (conn != null) { DbUtils.commitAndClose(conn); } } return affectedRows; }
From source file:nl.b3p.catalog.arcgis.ArcSDE9xJDBCHelper.java
@Override public void saveMetadata(ArcSDEJDBCDataset dataset, String metadata) throws Exception { Connection c = getConnection(); PreparedStatement ps = null;/* w w w . java2 s.co m*/ try { c.setAutoCommit(false); // gebruik geen DbUtils; setBinaryStream() werkt niet met setObject() // welke DbUtils gebruikt String sql = "update " + getTableName(TABLE_USERMETADATA) + " set xml = ? where name = ? and owner = ?"; sql += databaseNameSQL(dataset); ps = c.prepareStatement(sql); byte[] xml = metadata.getBytes(ENCODING); ps.setBinaryStream(1, new ByteArrayInputStream(xml), xml.length); ps.setString(2, dataset.getName()); ps.setString(3, dataset.getOwner()); if (dataset.getDatabaseName() != null) { ps.setString(4, dataset.getDatabaseName()); } int rowsAffected = ps.executeUpdate(); ps.close(); ps = null; if (rowsAffected > 1) { throw new Exception("Updating metadata should affect maximum one row; got rows affected count of " + rowsAffected); } if (rowsAffected == 0) { // try to insert new row QueryRunner runner = new QueryRunner(); // determine highest id Object id = runner.query(c, "select coalesce(max(id)+1,1) from " + getTableName(TABLE_USERMETADATA), new ScalarHandler()); Integer datasetType = determineDatasetType(c, dataset); // weer setBinaryStream nodig ps = c.prepareStatement("insert into " + getTableName(TABLE_USERMETADATA) + " (id, databasename, owner, name, datasettype, xml) values(?,?,?,?,?,?)"); ps.setObject(1, id); ps.setObject(2, dataset.getDatabaseName()); ps.setString(3, dataset.getOwner()); ps.setString(4, dataset.getName()); ps.setObject(5, datasetType); ps.setBinaryStream(6, new ByteArrayInputStream(xml), xml.length); ps.executeUpdate(); ps.close(); ps = null; } DbUtils.commitAndClose(c); } catch (Exception e) { DbUtils.rollbackAndCloseQuietly(c); throw e; } finally { DbUtils.closeQuietly(ps); } }
From source file:org.culturegraph.mf.sql.util.JdbcUtil.java
public static void closeConnection(final Connection connection) { try {//from ww w .j a va 2 s. com DbUtils.commitAndClose(connection); } catch (SQLException e) { e.printStackTrace(); } }
From source file:org.gaixie.jibu.JibuTestSupport.java
/** * ????/*from w w w . ja va 2 s . com*/ * ?? */ protected void clearTable() { Connection conn = null; try { conn = ConnectionUtils.getConnection(); QueryRunner run = new QueryRunner(); run.update(conn, "DELETE from schema_changes"); DbUtils.commitAndClose(conn); } catch (SQLException e) { DbUtils.rollbackAndCloseQuietly(conn); System.out.println(e.getMessage()); } }
From source file:org.gaixie.jibu.security.dao.SchemaCreate.java
/** * ?? dbscripts ? type???? sql /*from www . j a v a2s. c o m*/ * <p> * @param type ? jibu.properties ? */ public void create(String type) { Connection conn = null; try { conn = ConnectionUtils.getConnection(); DatabaseMetaData dbm = conn.getMetaData(); ResultSet rs = dbm.getTables(null, null, "USERBASE", null); if (rs.next()) throw new SQLException("Schema has been created!"); String dpn = dbm.getDatabaseProductName(); if (!"Apache Derby".equals(dpn)) throw new SQLException("Database is not Apache Derby!"); QueryRunner run = new QueryRunner(); URL url = this.getClass().getResource("/dbscripts/" + type + "/"); File dir = new File(url.toURI()); File[] files = dir.listFiles(); Arrays.sort(files); for (File file : files) { if (file.isFile()) { handleFile(run, conn, "/dbscripts/" + type + "/" + file.getName()); } } DbUtils.commitAndClose(conn); } catch (SQLException se) { DbUtils.rollbackAndCloseQuietly(conn); logger.warn("Schema create failed: " + se.getMessage()); } catch (IOException ie) { DbUtils.rollbackAndCloseQuietly(conn); logger.warn("Read SQL Scripts failed: " + ie.getMessage()); } catch (URISyntaxException e) { DbUtils.rollbackAndCloseQuietly(conn); logger.warn("Get SQL Scripts Directory failed: " + e.getMessage()); } }