Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java	(revision 21320)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java	(revision 21321)
@@ -24,5 +24,4 @@
     protected SQLStatements sqlState = null;
     protected Connection connection = null; 
-    protected Statement statement = null;
     protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
 
@@ -58,11 +57,10 @@
 
     public boolean openConnection(String databasepath){
+	if(connection != null) return true;
+
 	connection = sqlServer.connect(databasepath);
-	
 	if (connection == null) return false;
-
 	try{
-		connection.setAutoCommit(false);	
-		statement = connection.createStatement();
+	    connection.setAutoCommit(true);	
 	}
 	catch(SQLException sqle){
@@ -74,6 +72,7 @@
 
     public boolean openAndCreateConnection(String databasepath){
+	if(connection != null) return true;
     	connection = sqlServer.connectAndCreate(databasepath);
-
+	
     	if (connection == null) {
     		logger.error("sql connection is null. database path="+databasepath);
@@ -82,13 +81,6 @@
 
     	try{
-    		connection.setAutoCommit(true);	
-    		//by passing the two arguments, the ResultSet returned from executeQuery is updatable
-    		statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
-        	if (statement == null) {
-        		logger.error("statement is null");
-        		return false;
-        	}
-
-    	}
+	    connection.setAutoCommit(true);	
+	}
     	catch(SQLException sqle){
     		logger.debug("Database Error occured when creating a statement object",sqle);
@@ -103,11 +95,7 @@
 	ResultSet rs = null;
 	try{	
-	    
-	    if (statement == null){
-    		logger.info("the database hasn't been correct yet");
-    		return new ArrayList();
-	    }
-	    
-	    rs = statement.executeQuery(query_statement);
+	    //by passing the two arguments, the ResultSet returned from executeQuery is updatable
+	    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+	   rs = statement.executeQuery(query_statement);
 	    ResultSetMetaData rsmd = rs.getMetaData();
 	    int numOfColumns = rsmd.getColumnCount();
@@ -119,7 +107,5 @@
 		results.add(arow);  
 	    }
-
-	    rs.close();
-	   
+	    statement.close();
 	}
 	catch(SQLException sqle){
@@ -140,5 +126,8 @@
     public synchronized ResultSet queryResultSet(String stat){
     	ResultSet rs = null;
+	//by passing the two arguments, the ResultSet returned from executeQuery is updatable
+
     	try{	       	
+	    	Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
     		if (statement == null){
     			logger.info("Null sql statement provided.");
@@ -165,5 +154,7 @@
     public synchronized boolean execute(String stat) {
     	boolean rs;
+
     	try{	       	
+	    Statement statement = connection.createStatement();
     		if (statement == null){
     			logger.info("statement is null.");
@@ -173,6 +164,5 @@
     		rs = statement.execute(stat);
 		statement.close();
-		//    		connection.commit();
-    	}
+	}
     	catch(SQLException sqle){    		
     		logger.debug("Database Error occured when execute query " + stat, sqle);
@@ -190,5 +180,6 @@
     public synchronized boolean executeUpdate(String stat) {
     	int rs;
-    	try{	       	
+	try{	       	
+	    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
     		if (statement == null){
     			logger.info("statement is null.");
@@ -199,5 +190,4 @@
     		rs = statement.executeUpdate(stat);
 		statement.close();
-//    		connection.commit();
 		logger.debug("sql stat="+stat+ " result="+rs);
     	}
@@ -218,5 +208,6 @@
      */
     public void check4Table(String stat) throws SQLException {
-    	statement.executeQuery(stat);
+	Statement statement = connection.createStatement();
+   	statement.executeQuery(stat);
 	statement.close();
     }
@@ -224,7 +215,9 @@
     public void closeConnection(String databasepath){
 	try{
-	    statement.close();
-	    connection.close();
-	    sqlServer.disconnect(databasepath);
+	    if(connection != null){
+		connection.close();
+		connection = null;
+		sqlServer.disconnect(databasepath);
+	    }
 	}
 	catch(SQLException sqle){
