Improved database collector
diff --git a/src/main/java/de/ids_mannheim/korap/KorapIndex.java b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
index 12a6a76..8d5b93d 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
@@ -1027,12 +1027,11 @@
      * Search in the index.
      */
     public KorapResult search (SpanQuery query) {
-	return this.search(new KorapCollection(this), new KorapSearch(query));
+	return this.search(new KorapSearch(query));
     };
 
     public KorapResult search (SpanQuery query, short count) {
 	return this.search(
-	    new KorapCollection(this),
 	    new KorapSearch(query).setCount(count)
         );
     };
@@ -1050,12 +1049,7 @@
 	KorapSearch ks = new KorapSearch(query);
 	ks.setStartIndex(startIndex).setCount(count);
 	ks.setContext(new SearchContext(leftTokenContext, leftContext, rightTokenContext, rightContext));	
-	return this.search(new KorapCollection(this), ks);
-    };
-
-    public KorapResult search (KorapSearch ks) {
-	// TODO: This might leak
-	return this.search(new KorapCollection(this), ks);
+	return this.search(ks);
     };
 
     @Deprecated
@@ -1068,17 +1062,28 @@
 			       boolean rightTokenContext,
 			       short rightContext) {
 	KorapSearch ks = new KorapSearch(query);
-	ks.setContext(new SearchContext(leftTokenContext, leftContext, rightTokenContext, rightContext));	
-	return this.search(collection, ks);
+	ks.setContext(
+            new SearchContext(
+                leftTokenContext,
+		leftContext,
+		rightTokenContext,
+		rightContext
+            )
+        );
+	ks.setCollection(collection);
+	return this.search(ks);
     };
 
     // To be honest - the collection is already part of the KorapSearch! 
-    public KorapResult search (KorapCollection collection, KorapSearch ks) {
+    public KorapResult search (KorapSearch ks) {
 	if (DEBUG)
 	    log.trace("Start search");
 
 	this.termContexts = new HashMap<Term, TermContext>();
 
+	KorapCollection collection = ks.getCollection();
+	collection.setIndex(this);
+
 	// Get the spanquery from the KorapSearch object
 	SpanQuery query = ks.getQuery();
 
@@ -1275,12 +1280,13 @@
 
 
     // Collect matches
-    public MatchCollector collect (KorapCollection collection,
-				   KorapSearch ks,
-				   MatchCollector mc) {
+    public MatchCollector collect (KorapSearch ks, MatchCollector mc) {
 	if (DEBUG)
 	    log.trace("Start collecting");
 
+	KorapCollection collection = ks.getCollection();
+	collection.setIndex(this);
+
 	// Init term context
 	this.termContexts = new HashMap<Term, TermContext>();
 
@@ -1377,7 +1383,7 @@
 	    log.warn(e.getLocalizedMessage());
 	};
 
-	mc.commit();
+	mc.close();
 	return mc; 
     };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/KorapSearch.java b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
index c882ae4..7b2ff73 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapSearch.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
@@ -289,7 +289,7 @@
 	};
 
 	this.getCollection().setIndex(ki);
-	KorapResult kr = ki.search(this.getCollection(), this);
+	KorapResult kr = ki.search(this);
 	kr.setRequest(this.request);
 	if (this.warning != null)
 	    kr.addWarning(this.warning);
diff --git a/src/main/java/de/ids_mannheim/korap/index/MatchCollector.java b/src/main/java/de/ids_mannheim/korap/index/MatchCollector.java
index e1e93e3..ca3f056 100644
--- a/src/main/java/de/ids_mannheim/korap/index/MatchCollector.java
+++ b/src/main/java/de/ids_mannheim/korap/index/MatchCollector.java
@@ -26,7 +26,7 @@
     };
 
     public void commit() {};
-
+    public void close() {};
 
     /*
      * The following methods are shared and should be used from KorapResult
diff --git a/src/main/java/de/ids_mannheim/korap/index/collector/MatchCollectorDB.java b/src/main/java/de/ids_mannheim/korap/index/collector/MatchCollectorDB.java
index b7d212e..0dbe73c 100644
--- a/src/main/java/de/ids_mannheim/korap/index/collector/MatchCollectorDB.java
+++ b/src/main/java/de/ids_mannheim/korap/index/collector/MatchCollectorDB.java
@@ -1,6 +1,11 @@
 package de.ids_mannheim.korap.index.collector;
 import de.ids_mannheim.korap.KorapMatch;
 import de.ids_mannheim.korap.index.MatchCollector;
+import com.fasterxml.jackson.annotation.*;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import javax.sql.DataSource;
+import java.sql.SQLException;
 import java.util.*;
 
 public class MatchCollectorDB extends MatchCollector {
@@ -9,35 +14,79 @@
       Todo: In case there are multiple threads searching,
       the list should be synchrinized Collections.synchronizedList()
      */
+    private String databaseType;
     private List matchCollector;
     private int bufferSize;
-    private int doccollect;
+    private int docCollect;
+    private String resultID;
 
-    private String tableName;
+    private Connection connection;
+    private PreparedStatement prepared;
 
     /*
      * Create a new collector for database connections
      */
-    public MatchCollectorDB (int bufferSize, String tableName) {
+    public MatchCollectorDB (int bufferSize, String resultID) {
 	this.bufferSize = bufferSize;
-	this.tableName = tableName;
+	this.resultID = resultID;
 	this.matchCollector = new ArrayList<int[]>(bufferSize + 2);
     };
 
     /*
      * Add matches till the bufferSize exceeds - then commit to the database.
      */
-    public void add (int uniqueDocID, int matchcount) {
+    public void add (int UID, int matchCount) {
 	this.incrTotalResultDocs(1);
-	this.incrTotalResults(matchcount);
-	this.matchCollector.add(new int[]{uniqueDocID, matchcount});
-	if (this.doccollect++ > bufferSize)
+	this.incrTotalResults(matchCount);
+	this.matchCollector.add(new int[]{UID, matchCount});
+
+	if (this.docCollect++ > bufferSize)
 	    this.commit();
     };
 
-    public void commit () {
-	
+    @JsonIgnore
+    public void setDatabaseType (String type) {
+	this.databaseType = type;
+    };
+
+    @JsonIgnore
+    public String getDatabaseType () {
+	return this.databaseType;
+    };
+
+    @JsonIgnore
+    public void openConnection (String type, DataSource ds) throws SQLException {
+	this.setDatabaseType(type);
+	this.connection = ds.getConnection();
+	this.connection.setAutoCommit(false);
+
+	// Create prepared statement for multiple requests
+
+	/*
+	this.prepared = this.conn.prepareStatement(
+	  "INSERT INTO people VALUES (?, ?);"
+        );
+
+	Only prepare if commit > buffersize!
+Difference between mariadb and sqlite!
+	*/
+
+    };
+    
+    public void commit () {	
+
+	/*
+	 */
 	this.matchCollector.clear();
-	this.doccollect = 0;
+	this.docCollect = 0;
+    };
+
+    public void close () {
+	this.commit();
+	try {
+	    this.connection.close();
+	}
+	catch (SQLException e) {
+	};
     };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index eb0119a..818b6c3 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -208,12 +208,11 @@
      *
      * @param text_id
      */
-    /*
     @POST
     @Path("/collect/{resultID}")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_JSON)
-    public String find (String json, @Context UriInfo uri) {
+    public String collect (String json, @Context UriInfo uri) {
 
 	// Get index
 	KorapIndex index = KorapNode.getIndex();
@@ -225,14 +224,17 @@
 	        index.getVersion()
             ).setError(601, "Unable to find index").toJSON();
 
+
+	return "";
+
 	// Get the database
 	// Create a database based matchcollector
 
 
 	// TODO: Only search in self documents (REPLICATION FTW!)
 
-	MatchCollector result = index.collect(KorapCollection, KorapSearch, MatchCollector);
-
+	//MatchCollector result = index.collect(KorapCollection, KorapSearch, MatchCollector);
+	/*
 
 		// Build Collection based on a list of uids
 		List<String> uids = qp.get("uid");
@@ -259,8 +261,8 @@
 	    KorapNode.getName(),
 	    index.getVersion()
         ).setError(601, "Unable to find index").toJSON();
-    };
 */
+    };
 
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestDatabase.java b/src/test/java/de/ids_mannheim/korap/server/TestDatabase.java
index 04e7603..80276cf 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestDatabase.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestDatabase.java
@@ -10,9 +10,7 @@
 import java.sql.Statement;
 import java.sql.SQLException;
 
-/*
-  bitbucket.org/xerial/sqlite-jdbc
-*/
+import com.mchange.v2.c3p0.*;
 
 import org.junit.After;
 import org.junit.Before;
@@ -66,6 +64,64 @@
     };
 
     @Test
+    public void TestDatabasePool () throws Exception {
+	ComboPooledDataSource cpds = new ComboPooledDataSource();
+	// Connect to a temporary file instead of a in-memory file
+	cpds.setDriverClass("org.sqlite.JDBC");
+	cpds.setJdbcUrl("jdbc:sqlite:");
+	// cpds.setUser(xxx);
+	// cpds.setPassword(xxx);
+	cpds.setMaxStatements(100);
+	// cpds.autoCommitOnClose(true);
+	// cpds.aquireRetryDelay(100);
+
+	// This is part of the match collector
+	this.conn = cpds.getConnection();
+	this.stat = conn.createStatement();
+	stat.executeUpdate(
+            "CREATE TABLE IF NOT EXISTS result_a (text_id INTEGER, match_count INTEGER);"
+	);
+	conn.setAutoCommit(false);
+	PreparedStatement prep = this.conn.prepareStatement(
+	  "INSERT INTO result_a VALUES (?, ?);"
+        );
+	prep.setInt(1, 5);
+	prep.setInt(2, 8000);
+	prep.addBatch();
+	prep.executeBatch();
+
+	ResultSet rs = stat.executeQuery("SELECT * FROM result_a;");
+
+	rs.next();
+	
+	assertEquals(rs.getInt("text_id"), 5);
+	assertEquals(rs.getInt("match_count"), 8000);
+
+	rs.close();
+
+	this.conn.close();
+
+
+	MatchCollectorDB mc = new MatchCollectorDB(2000, "result_a");
+	mc.openConnection("sqlite", cpds);
+
+
+	mc.add(9, 5000);
+	mc.add(12, 6785);
+	mc.add(39, 56576);
+
+	mc.close();
+
+	cpds.close();
+
+	/*
+	this.stat = this.conn.createStatement();
+	stat.executeUpdate("CREATE TABLE IF NOT EXISTS result_a (text_id INTEGER, match_count INTEGER);");
+	*/
+    };
+
+
+    @Test
     public void TestMatchCollectorDB () throws Exception {
 	MatchCollector mc = new MatchCollectorDB(2000, "matchXYZ");
 	mc.add(5,7);