Author: reschke
Date: Tue Jan 21 18:40:58 2014
New Revision: 1560135

URL: http://svn.apache.org/r1560135
Log:
OAK-1266 - add DataSource based constructors

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLBlobStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLDocumentStore.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLBlobStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLBlobStore.java?rev=1560135&r1=1560134&r2=1560135&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLBlobStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLBlobStore.java
 Tue Jan 21 18:40:58 2014
@@ -29,6 +29,8 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.blobs.AbstractBlobStore;
 import org.apache.jackrabbit.mk.util.StringUtils;
 
+import sun.jdbc.odbc.ee.DataSource;
+
 // mostly copied from DbBlobStore, removing the ConnectionPool dependency
 
 public class SQLBlobStore extends AbstractBlobStore {
@@ -40,7 +42,8 @@ public class SQLBlobStore extends Abstra
     public SQLBlobStore() {
         try {
             String jdbcurl = "jdbc:h2:mem:oaknodes";
-            initialize(jdbcurl, "sa", "");
+            Connection connection = DriverManager.getConnection(jdbcurl, "sa", 
"");
+            initialize(connection);
         } catch (Exception ex) {
             throw new MicroKernelException("initializing SQL blob store", ex);
         }
@@ -52,7 +55,20 @@ public class SQLBlobStore extends Abstra
      */
     public SQLBlobStore(String jdbcurl, String username, String password) {
         try {
-            initialize(jdbcurl, username, password);
+            Connection connection = DriverManager.getConnection(jdbcurl, 
username, password);
+            initialize(connection);
+        } catch (Exception ex) {
+            throw new MicroKernelException("initializing SQL blob store", ex);
+        }
+    }
+
+    /**
+     * Creates a {@linkplain SQLBlobStore} instance using the provided
+     * {@link DataSource}.
+     */
+    public SQLBlobStore(DataSource ds) {
+        try {
+            initialize(ds.getConnection());
         } catch (Exception ex) {
             throw new MicroKernelException("initializing SQL blob store", ex);
         }
@@ -69,14 +85,17 @@ public class SQLBlobStore extends Abstra
 
     private Connection connection;
 
-    private void initialize(String jdbcurl, String username, String password) 
throws Exception {
-        connection = DriverManager.getConnection(jdbcurl, username, password);
-        connection.setAutoCommit(false);
-        Statement stmt = connection.createStatement();
+    private void initialize(Connection con) throws Exception {
+        con.setAutoCommit(false);
 
+        Statement stmt = con.createStatement();
         stmt.execute("create table if not exists datastore_meta" + "(id 
varchar primary key, level int, lastMod bigint)");
         stmt.execute("create table if not exists datastore_data" + "(id 
varchar primary key, data binary)");
         stmt.close();
+
+        con.commit();
+
+        this.connection = con;
     }
 
     private long minLastModified;

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLDocumentStore.java?rev=1560135&r1=1560134&r2=1560135&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/sqlpersistence/SQLDocumentStore.java
 Tue Jan 21 18:40:58 2014
@@ -32,6 +32,7 @@ import java.util.TreeMap;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
+import javax.sql.DataSource;
 
 import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.oak.plugins.mongomk.Collection;
@@ -56,7 +57,19 @@ public class SQLDocumentStore implements
     public SQLDocumentStore() {
         try {
             String jdbcurl = "jdbc:h2:mem:oaknodes";
-            initialize(jdbcurl, "sa", "");
+            Connection connection = DriverManager.getConnection(jdbcurl, "sa", 
"");
+            initialize(connection);
+        } catch (Exception ex) {
+            throw new MicroKernelException("initializing SQL document store", 
ex);
+        }
+    }
+
+    /**
+     * Creates a {@linkplain SQLDocumentStore} instance using the provided 
{@link DataSource}.
+     */
+    public SQLDocumentStore(DataSource ds) {
+        try {
+            initialize(ds.getConnection());
         } catch (Exception ex) {
             throw new MicroKernelException("initializing SQL document store", 
ex);
         }
@@ -68,7 +81,8 @@ public class SQLDocumentStore implements
      */
     public SQLDocumentStore(String jdbcurl, String username, String password) {
         try {
-            initialize(jdbcurl, username, password);
+            Connection connection = DriverManager.getConnection(jdbcurl, 
username, password);
+            initialize(connection);
         } catch (Exception ex) {
             throw new MicroKernelException("initializing SQL document store", 
ex);
         }
@@ -157,15 +171,17 @@ public class SQLDocumentStore implements
 
     private Connection connection;
 
-    private void initialize(String jdbcurl, String username, String password) 
throws Exception {
-        connection = DriverManager.getConnection(jdbcurl, username, password);
-        connection.setAutoCommit(false);
-        Statement stmt = connection.createStatement();
+    private void initialize(Connection con) throws Exception {
+        con.setAutoCommit(false);
 
+        Statement stmt = con.createStatement();
         stmt.execute("create table if not exists CLUSTERNODES(ID varchar 
primary key, MODIFIED bigint, MODCOUNT bigint, DATA varchar)");
         stmt.execute("create table if not exists NODES(ID varchar primary key, 
MODIFIED bigint, MODCOUNT bigint, DATA varchar)");
+        stmt.close();
+
+        con.commit();
 
-        connection.commit();
+        this.connection = con;
     }
 
     @CheckForNull


Reply via email to