List of usage examples for org.apache.commons.dbutils DbUtils commitAndCloseQuietly
public static void commitAndCloseQuietly(Connection conn)
Connection
then closes it, avoid closing if null and hide any SQLExceptions that occur. From source file:com.splicemachine.derby.test.framework.SpliceViewWatcher.java
@Override public void starting(Description description) { LOG.trace("Starting"); Connection connection = null; Statement statement = null;/*w w w .j a v a 2 s . c om*/ ResultSet rs = null; try { connection = userName == null ? SpliceNetConnection.getConnection() : SpliceNetConnection.getConnectionAs(userName, password); rs = connection.getMetaData().getTables(null, schemaName, viewName, null); if (rs.next()) { executeDrop(schemaName, viewName); } connection.commit(); statement = connection.createStatement(); statement.execute(CREATE_VIEW + schemaName + "." + viewName + " " + createString); connection.commit(); } catch (Exception e) { LOG.error("Create view statement is invalid "); e.printStackTrace(System.err); throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(statement); DbUtils.commitAndCloseQuietly(connection); } super.starting(description); }
From source file:com.splicemachine.derby.test.framework.SpliceTableWatcher.java
public void importData(String filename, String timestamp) { Connection connection = null; PreparedStatement ps = null;/* www . j a v a 2 s . c o m*/ try { connection = SpliceNetConnection.getConnection(); ps = connection.prepareStatement( "call SYSCS_UTIL.IMPORT_DATA (?, ?, null,?,',',null,?,null,null,0,null,true,null)"); ps.setString(1, schemaName); ps.setString(2, tableName); ps.setString(3, filename); ps.setString(4, timestamp); ps.executeQuery(); } catch (Exception e) { throw new RuntimeException(e); } finally { DbUtils.closeQuietly(ps); DbUtils.commitAndCloseQuietly(connection); } }
From source file:com.splicemachine.derby.test.framework.SpliceIndexWatcher.java
@Override public void starting(Description description) { LOG.trace("Starting"); Connection connection = null; PreparedStatement statement = null; Statement statement2 = null;/*from w w w . j a va 2s.co m*/ ResultSet rs = null; try { connection = SpliceNetConnection.getConnection(); statement = connection.prepareStatement(SELECT_SPECIFIC_INDEX); statement.setString(1, indexSchemaName); statement.setString(2, indexName); rs = statement.executeQuery(); if (rs.next()) { executeDrop(connection, indexSchemaName, indexName); } connection.commit(); statement2 = connection.createStatement(); statement2.execute(String.format("%s index %s.%s on %s.%s %s", create, indexSchemaName, indexName, tableSchemaName, tableName, createString)); connection.commit(); } catch (Exception e) { LOG.error("Create index statement is invalid "); throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(statement); DbUtils.closeQuietly(statement2); DbUtils.commitAndCloseQuietly(connection); } super.starting(description); }
From source file:com.splicemachine.derby.test.framework.SpliceUserWatcher.java
public void dropUser(String userName) { Connection connection = null; PreparedStatement statement = null; try {//from ww w . jav a 2 s . co m connection = SpliceNetConnection.getConnection(); statement = connection.prepareStatement("select username from sys.sysusers where username = ?"); statement.setString(1, userName.toUpperCase()); ResultSet rs = statement.executeQuery(); if (rs.next()) { statement = connection.prepareStatement("call syscs_util.syscs_drop_user(?)"); statement.setString(1, userName); statement.execute(); } } catch (Exception e) { LOG.error("error Creating " + e.getMessage()); e.printStackTrace(); throw new RuntimeException(e); } finally { DbUtils.closeQuietly(statement); DbUtils.commitAndCloseQuietly(connection); } }
From source file:com.splicemachine.derby.test.framework.SpliceTableWatcher.java
public void start() { Statement statement = null;/*from w ww . j a v a2 s .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:azkaban.database.AzkabanDatabaseSetup.java
private void loadInstalledTables() throws SQLException { logger.info("Searching for installed tables"); Connection conn = null;/*from w w w. j av a 2s. co m*/ try { conn = dataSource.getConnection(); ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), null, null, new String[] { "TABLE" }); while (rs.next()) { tables.put(rs.getString("TABLE_NAME").toLowerCase(), "2.1"); } } finally { DbUtils.commitAndCloseQuietly(conn); } }
From source file:org.moneta.dao.SqlSelectExecutor.java
public SearchResult call() { SearchResult result = new SearchResult(); Connection topicConnection = null; QueryRunner runner = new QueryRunner(); try {// w w w . j ava 2 s .com topicConnection = MonetaEnvironment.getConfiguration().getConnection(topic.getDataSourceName()); RecordResultSetHandler handler = new RecordResultSetHandler(); handler.setMaxRows(this.getMaxRows()); handler.setStartRow(this.getStartRow()); handler.getAliasMap().putAll(topic.getAliasMap()); result.setResultData(runner.query(topicConnection, sqlStmt.getSqlText(), handler, sqlStmt.getHostVariableValueList().toArray())); if (topicConnection.getAutoCommit()) { DbUtils.closeQuietly(topicConnection); } else { DbUtils.commitAndCloseQuietly(topicConnection); } } catch (Exception e) { result.setErrorCode(500); result.setErrorMessage(ExceptionUtils.getStackTrace(e)); DbUtils.rollbackAndCloseQuietly(topicConnection); } return result; }
From source file:org.sonar.core.persistence.DbTester.java
public void executeUpdateSql(String sql) { Connection connection = null; try {//from ww w. j a v a2 s . c om connection = openConnection(); new QueryRunner().update(connection, sql); } catch (Exception e) { throw new IllegalStateException("Fail to execute sql: " + sql); } finally { DbUtils.commitAndCloseQuietly(connection); } }
From source file:org.sonar.db.version.v50.FeedFileSourcesTest.java
private void migrate_sources_with_scm_and_coverage_in(String columnName) throws Exception { db.prepareDbUnit(getClass(), "before.xml"); Connection connection = null; try {/*from w w w . j ava 2s . c o m*/ connection = db.openConnection(); db.executeUpdateSql("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values " + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')"); db.executeUpdateSql("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values " + "(7, '', '2014-10-31 16:44:02.000')"); PreparedStatement revisionStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(1, 6, ?)"); revisionStmt.setBytes(1, "1=aef12a;2=abe465;3=afb789;4=afb789".getBytes(StandardCharsets.UTF_8)); revisionStmt.executeUpdate(); revisionStmt.close(); PreparedStatement authorStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(2, 6, ?)"); authorStmt.setBytes(1, "1=alice;2=bob;3=carol;4=carol".getBytes(StandardCharsets.UTF_8)); authorStmt.executeUpdate(); authorStmt.close(); PreparedStatement dateStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(3, 6, ?)"); dateStmt.setBytes(1, "1=2014-04-25T12:34:56+0100;2=2014-07-25T12:34:56+0100;3=2014-03-23T12:34:56+0100;4=2014-03-23T12:34:56+0100" .getBytes(StandardCharsets.UTF_8)); dateStmt.executeUpdate(); dateStmt.close(); PreparedStatement utHitsStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(4, 6, ?)"); utHitsStmt.setBytes(1, "1=1;3=0".getBytes(StandardCharsets.UTF_8)); utHitsStmt.executeUpdate(); utHitsStmt.close(); PreparedStatement utCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(5, 6, ?)"); utCondStmt.setBytes(1, "1=4".getBytes(StandardCharsets.UTF_8)); utCondStmt.executeUpdate(); utCondStmt.close(); PreparedStatement utCoveredCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(6, 6, ?)"); utCoveredCondStmt.setBytes(1, "1=2".getBytes(StandardCharsets.UTF_8)); utCoveredCondStmt.executeUpdate(); utCoveredCondStmt.close(); PreparedStatement itHitsStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(7, 6, ?)"); itHitsStmt.setBytes(1, "1=2;3=0".getBytes(StandardCharsets.UTF_8)); itHitsStmt.executeUpdate(); itHitsStmt.close(); PreparedStatement itCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(8, 6, ?)"); itCondStmt.setBytes(1, "1=5".getBytes(StandardCharsets.UTF_8)); itCondStmt.executeUpdate(); itCondStmt.close(); PreparedStatement itCoveredCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(9, 6, ?)"); itCoveredCondStmt.setBytes(1, "1=3".getBytes(StandardCharsets.UTF_8)); itCoveredCondStmt.executeUpdate(); itCoveredCondStmt.close(); PreparedStatement overallHitsStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(10, 6, ?)"); overallHitsStmt.setBytes(1, "1=3;3=0".getBytes(StandardCharsets.UTF_8)); overallHitsStmt.executeUpdate(); overallHitsStmt.close(); PreparedStatement overallCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(11, 6, ?)"); overallCondStmt.setBytes(1, "1=6".getBytes(StandardCharsets.UTF_8)); overallCondStmt.executeUpdate(); overallCondStmt.close(); PreparedStatement overallCoveredCondStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(12, 6, ?)"); overallCoveredCondStmt.setBytes(1, "1=4".getBytes(StandardCharsets.UTF_8)); overallCoveredCondStmt.executeUpdate(); overallCoveredCondStmt.close(); PreparedStatement duplicationDataStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, " + columnName + ") " + "values " + "(13, 6, ?)"); duplicationDataStmt.setBytes(1, "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/></g></duplications>" .getBytes(StandardCharsets.UTF_8)); duplicationDataStmt.executeUpdate(); duplicationDataStmt.close(); } finally { DbUtils.commitAndCloseQuietly(connection); } migration.execute(); List<Map<String, Object>> results = getFileSources(); assertThat(results).hasSize(2); assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject"); assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo"); assertThat(results.get(0).get("data")).isEqualTo(""); assertThat(results.get(0).get("lineHashes")).isEqualTo(""); assertThat(results.get(0).get("dataHash")).isEqualTo(""); assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW); assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L); assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject"); assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo"); assertThat(results.get(1).get("data")).isEqualTo( "aef12a,alice,2014-04-25T12:34:56+0100,1,4,2,2,5,3,3,6,4,,,1,class Foo {\r\nabe465,bob,2014-07-25T12:34:56+0100,,,,,,,,,,,,2, " + "// Empty\r\nafb789,carol,2014-03-23T12:34:56+0100,0,,,0,,,0,,,,,,}\r\nafb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,\r\n"); assertThat(results.get(1).get("lineHashes")).isEqualTo( "6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n"); assertThat(results.get(1).get("dataHash")).isEqualTo(""); assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31"); assertThat(results.get(1).get("createdAt")).isEqualTo(NOW); }
From source file:org.sonar.db.version.v50.FeedFileSourcesTest.java
@Test public void migrate_sources_with_invalid_duplication() throws Exception { db.prepareDbUnit(getClass(), "before.xml"); Connection connection = null; PreparedStatement duplicationDataStmt = null; try {//from w ww .ja v a 2 s .co m connection = db.openConnection(); connection .prepareStatement("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values " + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')") .executeUpdate(); db.executeUpdateSql("insert into snapshot_sources " + "(snapshot_id, data, updated_at) " + "values " + "(7, '', '2014-10-31 16:44:02.000')"); duplicationDataStmt = connection.prepareStatement("insert into project_measures " + "(metric_id, snapshot_id, text_value) " + "values " + "(13, 6, ?)"); duplicationDataStmt.setBytes(1, "<duplications><g><b s=\"1\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"2\" l=\"1\" r=\"MyProject:src/main/xoo/prj/MyFile.xoo\"/><b s=\"3\" l=\"1\" r=\"MyProject:src/main/xoo/prj/AnotherFile.xoo\"/" .getBytes(StandardCharsets.UTF_8)); duplicationDataStmt.executeUpdate(); } finally { DbUtils.close(duplicationDataStmt); DbUtils.commitAndCloseQuietly(connection); } migration.execute(); // db.assertDbUnit(getClass(), "after-with-invalid-duplication.xml", "file_sources"); List<Map<String, Object>> results = getFileSources(); assertThat(results).hasSize(2); assertThat(results.get(0).get("projectUuid")).isEqualTo("uuid-MyProject"); assertThat(results.get(0).get("fileUuid")).isEqualTo("uuid-Migrated.xoo"); assertThat(results.get(0).get("data")).isEqualTo(""); assertThat(results.get(0).get("lineHashes")).isEqualTo(""); assertThat(results.get(0).get("dataHash")).isEqualTo(""); assertThat(results.get(0).get("updatedAt")).isEqualTo(NOW); assertThat(results.get(0).get("createdAt")).isEqualTo(1416238020000L); assertThat(results.get(1).get("projectUuid")).isEqualTo("uuid-MyProject"); assertThat(results.get(1).get("fileUuid")).isEqualTo("uuid-MyFile.xoo"); assertThat(results.get(1).get("data")).isEqualTo( ",,,,,,,,,,,,,,,class Foo {\r\n,,,,,,,,,,,,,,, // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n"); assertThat(results.get(1).get("lineHashes")).isEqualTo( "6a19ce786467960a3a9b0d26383a464a\naab2dbc5fdeaa80b050b1d049ede357c\ncbb184dd8e05c9709e5dcaedaa0495cf\n\n"); assertThat(results.get(1).get("dataHash")).isEqualTo(""); assertThat(formatLongDate((long) results.get(1).get("updatedAt")).toString()).startsWith("2014-10-31"); assertThat(results.get(1).get("createdAt")).isEqualTo(NOW); }