List of usage examples for java.sql Connection commit
void commit() throws SQLException;
Connection
object. From source file:de.unidue.inf.is.ezdl.dlservices.repository.store.repositories.DBRepository.java
@Override public StoredDocument getDocument(String oid) { Connection con = null; PreparedStatement st = null;/*from ww w . j a v a 2 s. c om*/ ResultSet res = null; StoredDocument out = null; final String databaseIdForOid = databaseIdForOid(oid); try { con = provider.connection(); st = con.prepareStatement(GET); st.setString(1, databaseIdForOid); res = st.executeQuery(); if (res.next()) { Blob clob = res.getBlob(1); out = decode(clob.getBinaryStream()); } con.commit(); } catch (SQLException e) { rollback(con); getLogger().error("Error selecting " + databaseIdForOid, e); } finally { ClosingUtils.close(res); ClosingUtils.close(st); ClosingUtils.close(con); } return out; }
From source file:com.cloudera.sqoop.testutil.HsqldbTestServer.java
/** * Fill the table with some data.//from w w w.j a v a2 s .c o m */ public void populateData() throws SQLException { Connection connection = null; Statement st = null; try { connection = getConnection(); st = connection.createStatement(); st.executeUpdate("INSERT INTO " + DUMMY_TABLE_NAME + " VALUES(1, 8)"); st.executeUpdate("INSERT INTO " + DUMMY_TABLE_NAME + " VALUES(3, 6)"); st.executeUpdate("INSERT INTO " + DUMMY_TABLE_NAME + " VALUES(5, 4)"); st.executeUpdate("INSERT INTO " + DUMMY_TABLE_NAME + " VALUES(7, 2)"); connection.commit(); } finally { if (null != st) { st.close(); } if (null != connection) { connection.close(); } } }
From source file:com.asakusafw.testdriver.TestDriverTestToolsBase.java
private void updateTimestamp(String tableName, String timestampColumn, Timestamp timestamp) { LOG.info("{}?{}??????", tableName, timestampColumn); Connection conn = null; PreparedStatement stmt = null; try {//from w w w . j a v a 2s .com conn = DbUtils.getConnection(); stmt = conn.prepareStatement( MessageFormat.format("UPDATE {0} SET {1} = ? WHERE {1} IS NULL", tableName, timestampColumn)); stmt.setTimestamp(1, timestamp); int rows = stmt.executeUpdate(); LOG.info("{}?{}?????: {}", new Object[] { tableName, timestampColumn, rows }); if (conn.getAutoCommit() == false) { conn.commit(); } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(conn); } }
From source file:com.netflix.metacat.usermetadata.mysql.MySqlTagService.java
@Override public Void rename(final QualifiedName name, final String newTableName) { try {// www.j a v a 2 s . c om final Connection conn = getDataSource().getConnection(); try { final QualifiedName newName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), newTableName); new QueryRunner().update(conn, SQL_UPDATE_TAG_ITEM, newName.toString(), name.toString()); conn.commit(); } catch (Exception e) { conn.rollback(); throw e; } finally { conn.close(); } } catch (SQLException e) { final String message = String.format("Failed to rename item name %s", name); log.error(message, e); throw new UserMetadataServiceException(message, e); } return null; }
From source file:com.octo.captcha.engine.bufferedengine.buffer.DatabaseCaptchaBuffer.java
/** * Clear the buffer from all locale/*ww w. ja v a 2 s.c om*/ */ public void clear() { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = datasource.getConnection(); ps = con.prepareStatement("delete from " + table); ps.execute(); con.commit(); } catch (SQLException e) { log.error(DB_ERROR, e); if (rs != null) { try { rs.close(); } catch (SQLException ex) { } } } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { } } if (con != null) { try { con.close(); } catch (SQLException e) { } } } }
From source file:com.splicemachine.derby.test.framework.SpliceTableWatcher.java
public void start() { Statement statement = null;//from ww w . j a va2s . c om ResultSet rs; Connection connection; synchronized (SpliceTableWatcher.class) { try { connection = (userName == null) ? SpliceNetConnection.getConnection() : SpliceNetConnection.getConnectionAs(userName, password); rs = connection.getMetaData().getTables(null, schemaName, tableName, null); } catch (Exception e) { throw new RuntimeException(e); } } TableDAO tableDAO = new TableDAO(connection); try { if (rs.next()) { tableDAO.drop(schemaName, tableName); } connection.commit(); } catch (SQLException e) { throw new RuntimeException(e); } try { statement = connection.createStatement(); statement.execute(String.format("create table %s.%s %s", schemaName, tableName, createString)); connection.commit(); } catch (Exception e) { throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(statement); DbUtils.commitAndCloseQuietly(connection); } }
From source file:edu.ucsb.cs.eager.dao.EagerDependencyMgtDAO.java
public void updateAPISpec(APIInfo api, String specification) throws EagerException { Connection conn = null; PreparedStatement ps = null;/*from www . j a v a 2s.c o m*/ String updateQuery = "UPDATE EAGER_API_SPEC SET EAGER_API_SPEC_TEXT=? WHERE " + "EAGER_API_NAME=? AND EAGER_API_VERSION=?"; try { conn = APIMgtDBUtil.getConnection(); ps = conn.prepareStatement(updateQuery); ps.setString(1, specification); ps.setString(2, api.getName()); ps.setString(3, api.getVersion()); ps.executeUpdate(); conn.commit(); } catch (SQLException e) { handleException("Error while recording API specification", e); } finally { APIMgtDBUtil.closeAllConnections(ps, conn, null); } }
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 . j a v a 2 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.cloudera.sqoop.manager.DirectMySQLExportTest.java
/** * Test an authenticated export using mysqlimport. */// w ww . java 2 s . c om public void testAuthExport() throws IOException, SQLException { SqoopOptions options = new SqoopOptions(MySQLAuthTest.AUTH_CONNECT_STRING, getTableName()); options.setUsername(MySQLAuthTest.AUTH_TEST_USER); options.setPassword(MySQLAuthTest.AUTH_TEST_PASS); manager = new DirectMySQLManager(options); Connection connection = null; Statement st = null; String tableName = getTableName(); try { connection = manager.getConnection(); connection.setAutoCommit(false); st = connection.createStatement(); // create a target database table. st.executeUpdate("DROP TABLE IF EXISTS " + tableName); st.executeUpdate("CREATE TABLE " + tableName + " (" + "id INT NOT NULL PRIMARY KEY, " + "msg VARCHAR(24) NOT NULL)"); connection.commit(); // Write a file containing a record to export. Path tablePath = getTablePath(); Path filePath = new Path(tablePath, "datafile"); Configuration conf = new Configuration(); conf.set("fs.default.name", "file:///"); FileSystem fs = FileSystem.get(conf); fs.mkdirs(tablePath); OutputStream os = fs.create(filePath); BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os)); w.write(getRecordLine(0)); w.write(getRecordLine(1)); w.write(getRecordLine(2)); w.close(); os.close(); // run the export and verify that the results are good. runExport(getArgv(true, 10, 10, "--username", MySQLAuthTest.AUTH_TEST_USER, "--password", MySQLAuthTest.AUTH_TEST_PASS, "--connect", MySQLAuthTest.AUTH_CONNECT_STRING)); verifyExport(3, connection); } catch (SQLException sqlE) { LOG.error("Encountered SQL Exception: " + sqlE); sqlE.printStackTrace(); fail("SQLException when accessing target table. " + sqlE); } finally { try { if (null != st) { st.close(); } if (null != connection) { connection.close(); } } catch (SQLException sqlE) { LOG.warn("Got SQLException when closing connection: " + sqlE); } } }
From source file:edu.ucsb.cs.eager.dao.EagerDependencyMgtDAO.java
public void saveAPISpec(APIInfo api, String specification) throws EagerException { Connection conn = null; PreparedStatement ps = null;/*from w ww .j a va2 s. com*/ String insertQuery = "INSERT INTO EAGER_API_SPEC (EAGER_API_NAME, EAGER_API_VERSION, " + "EAGER_API_SPEC_TEXT) VALUES (?,?,?)"; try { conn = APIMgtDBUtil.getConnection(); ps = conn.prepareStatement(insertQuery); ps.setString(1, api.getName()); ps.setString(2, api.getVersion()); ps.setString(3, specification); ps.executeUpdate(); conn.commit(); } catch (SQLException e) { handleException("Error while recording API specification", e); } finally { APIMgtDBUtil.closeAllConnections(ps, conn, null); } }