Merge "Fix Sonar issues in ATDB" into develop
diff --git a/plugins/org.eclipse.app4mc.atdb.import.amalthea/src/org/eclipse/app4mc/atdb/_import/amalthea/EventChainMetricCalculator.java b/plugins/org.eclipse.app4mc.atdb.import.amalthea/src/org/eclipse/app4mc/atdb/_import/amalthea/EventChainMetricCalculator.java
index 30d8541..c6afa03 100644
--- a/plugins/org.eclipse.app4mc.atdb.import.amalthea/src/org/eclipse/app4mc/atdb/_import/amalthea/EventChainMetricCalculator.java
+++ b/plugins/org.eclipse.app4mc.atdb.import.amalthea/src/org/eclipse/app4mc/atdb/_import/amalthea/EventChainMetricCalculator.java
@@ -29,8 +29,7 @@
 	@Override
 	public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
 		final SubMonitor subMon = SubMonitor.convert(progressMonitor, "Calculating event chain metrics...", 3);
-		try {
-			final Statement mStmt = this.con.createStatement();
+		try (final Statement mStmt = this.con.createStatement()) {
 			// create event chain instances with separate sql program (a sequence of queries)
 			String ecInstCalculationSQL = "";
 			final Bundle bundle = FrameworkUtil.getBundle(EventChainMetricCalculator.class);
diff --git a/plugins/org.eclipse.app4mc.atdb.import.btf/src/org/eclipse/app4mc/atdb/_import/btf/BTFImporter.java b/plugins/org.eclipse.app4mc.atdb.import.btf/src/org/eclipse/app4mc/atdb/_import/btf/BTFImporter.java
index 55d386f..fb8df8c 100644
--- a/plugins/org.eclipse.app4mc.atdb.import.btf/src/org/eclipse/app4mc/atdb/_import/btf/BTFImporter.java
+++ b/plugins/org.eclipse.app4mc.atdb.import.btf/src/org/eclipse/app4mc/atdb/_import/btf/BTFImporter.java
@@ -56,8 +56,9 @@
 		final SubMonitor readingBTFMonitor = subMon.split(5);
 		readingBTFMonitor.beginTask("Reading BTF file...", 10_000);
 		try (final FileInputStream fis = new FileInputStream(this.btfFile);
-				final Scanner sc = new Scanner(fis, "UTF-8")) {
-			final Statement propStmt = this.con.createStatement();
+				final Scanner sc = new Scanner(fis, "UTF-8");
+				final Statement propStmt = this.con.createStatement()) {
+			
 			final PreparedStatement metaStmt = this.con.getPreparedStatementFor("INSERT INTO metaInformation VALUES(?, ?);");
 			final PreparedStatement entTStmt = this.con.getPreparedStatementFor("INSERT INTO entityType(name) VALUES(?);");
 			final PreparedStatement entStmt = this.con.getPreparedStatementFor("INSERT INTO entity(name, entityTypeId) VALUES(?,"
diff --git a/plugins/org.eclipse.app4mc.atdb/src/org/eclipse/app4mc/atdb/DBConnection.java b/plugins/org.eclipse.app4mc.atdb/src/org/eclipse/app4mc/atdb/DBConnection.java
index 491ebf8..65d9ac9 100644
--- a/plugins/org.eclipse.app4mc.atdb/src/org/eclipse/app4mc/atdb/DBConnection.java
+++ b/plugins/org.eclipse.app4mc.atdb/src/org/eclipse/app4mc/atdb/DBConnection.java
@@ -26,7 +26,6 @@
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Properties;
 import java.util.Set;
 import java.util.stream.Stream;
@@ -37,7 +36,6 @@
 	private static final String TABLE_EXISTS_QUERY = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?;";
 	
 	private final Connection connection;
-	private final Optional<Statement> statement;
 	
 	private final Map<String, PreparedStatement> prepStmts;
 	private final Collection<Statement> stmts;
@@ -72,13 +70,12 @@
 			final Properties properties = new Properties();
 			properties.put("open_mode", "1"); //$NON-NLS-1$ //$NON-NLS-2$
 			this.connection = DriverManager.getConnection(dbFileUrl, properties);
-			this.statement = Optional.empty();
 		} else {
 			this.connection = DriverManager.getConnection(dbFileUrl);
-			final Statement tmpStatement = this.connection.createStatement();
-			tmpStatement.setQueryTimeout(30); // set timeout to 30 sec.
-			tmpStatement.executeUpdate("PRAGMA foreign_keys = ON;"); // enable foreign key checks
-			this.statement = Optional.of(tmpStatement);
+			try (final Statement statement = this.connection.createStatement()) {
+				statement.setQueryTimeout(30); // set timeout to 30 sec.
+				statement.executeUpdate("PRAGMA foreign_keys = ON;"); // enable foreign key checks
+			}
 		}
 		this.prepStmts = new LinkedHashMap<>();
 		this.stmts = new LinkedHashSet<>();
@@ -94,9 +91,6 @@
 			ps.close();
 		}
 		this.prepStmts.clear();
-		if (this.statement.isPresent()) {
-			this.statement.get().close();
-		}
 		this.connection.close();
 	}
 	
@@ -107,12 +101,10 @@
 	 * @return <code>TRUE</code> if the statement was executed by the db, <code>FALSE</code> otherwise.
 	 * @throws SQLException
 	 */
-	public boolean executeUpdate(final String query) throws SQLException {
-		if (this.statement.isPresent()) {
-			this.statement.get().executeUpdate(query);
-			return true;
-		} else {
-			return false;
+	public void executeUpdate(final String query) throws SQLException {
+		try (final Statement statement = this.connection.createStatement()) {
+			statement.setQueryTimeout(30); // set timeout to 30 sec.
+			statement.executeUpdate(query);
 		}
 	}
 	
@@ -163,12 +155,13 @@
 	 */
 	protected <R> Stream<R> queryAndMapToStream(final PreparedStatement query, final ThrowingFunction<ResultSet, R, SQLException> rowMapper)
 			throws SQLException {
-		final ResultSet resultSet = query.executeQuery();
-		final Builder<R> result = Stream.builder();
-		while (resultSet.next()) {
-			result.accept(rowMapper.apply(resultSet));
+		try (final ResultSet resultSet = query.executeQuery()) {
+			final Builder<R> result = Stream.builder();
+			while (resultSet.next()) {
+				result.accept(rowMapper.apply(resultSet));
+			}
+			return result.build();
 		}
-		return result.build();
 	}
 	
 	/**
@@ -254,8 +247,9 @@
 	public boolean tableExists(final String tableName) throws SQLException {
 		final PreparedStatement prepStmt = getPrepareQueryFor(TABLE_EXISTS_QUERY);
 		prepStmt.setString(1, tableName);
-		final ResultSet resultSet = prepStmt.executeQuery();
-		return resultSet.next();
+		try (final ResultSet resultSet = prepStmt.executeQuery()) {
+			return resultSet.next();
+		}
 	}
 	
 	/**