From e1956bb6bd38d62b0ebe6f735a400cf793ad8b27 Mon Sep 17 00:00:00 2001
From: Richard Cordovano <rcordovano@basistech.com>
Date: Wed, 23 Oct 2013 13:23:05 -0400
Subject: [PATCH] Improved/fixed schema update code

---
 .../src/org/sleuthkit/datamodel/SleuthkitCase.java  | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
index e1065b608..bb0a56072 100644
--- a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
+++ b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
@@ -62,7 +62,6 @@ public class SleuthkitCase {
 	private String dbDirPath;
 	private volatile SleuthkitJNI.CaseDbHandle caseHandle;
 	private volatile Connection con;
-	private int schemaVersionNumber = 0;
 	private ResultSetHelper rsHelper = new ResultSetHelper(this);
 	private int artifactIDcounter = 1001;
 	private int attributeIDcounter = 1001;
@@ -154,6 +153,7 @@ private void updateDatabaseSchema() throws TskCoreException {
 			con.setAutoCommit(false);
 
 			// Get the schema version.
+			int schemaVersionNumber = 0;
 			Statement statement = con.createStatement();
 			ResultSet resultSet = statement.executeQuery("SELECT schema_ver FROM tsk_db_info");
 			if (resultSet.next()) {
@@ -161,8 +161,8 @@ private void updateDatabaseSchema() throws TskCoreException {
 			}
 			resultSet.close();
 						
-			// Call schema update methods here.
-			updateFromSchema3toSchema4();		
+			// Call schema update methods here. These methods should update schemaVersionNumber.
+			schemaVersionNumber = updateFromSchema3toSchema4(schemaVersionNumber);		
 			
 			// Update the schema version.
 			statement.executeUpdate("UPDATE tsk_db_info SET schema_ver = " + schemaVersionNumber);
@@ -174,6 +174,7 @@ private void updateDatabaseSchema() throws TskCoreException {
 		catch (Exception ex) {
 			try {
 				con.rollback();
+				con.setAutoCommit(true);
 				throw new TskCoreException("Failed to update database schema", ex);
 			}
 			catch (SQLException e) {
@@ -182,9 +183,9 @@ private void updateDatabaseSchema() throws TskCoreException {
 		}
 	}
 		
-	private void updateFromSchema3toSchema4() throws SQLException, TskCoreException {
+	private int updateFromSchema3toSchema4(int schemaVersionNumber) throws SQLException, TskCoreException {
 		if (schemaVersionNumber != 3) {
-			return;
+			return schemaVersionNumber;
 		}
 
 		// Add new tables for tags.
@@ -257,7 +258,7 @@ else if (attribute.getAttributeTypeID() == ATTRIBUTE_TYPE.TSK_COMMENT.getTypeID(
 				
 		closeStatements();
 		
-		schemaVersionNumber = 4;	
+		return 4;	
 	}			
 				
 	/**
-- 
GitLab