You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by en...@apache.org on 2014/01/15 04:10:03 UTC
git commit: CLEREZZA-866: Changed the way jdbc connections are
managed. Some code cleanup to do.
Updated Branches:
refs/heads/master d0afddb50 -> 5a2fdcebf
CLEREZZA-866: Changed the way jdbc connections are managed. Some code cleanup to do.
Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/5a2fdceb
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/5a2fdceb
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/5a2fdceb
Branch: refs/heads/master
Commit: 5a2fdcebfdb4df5179039ee7068408ebe132ab28
Parents: d0afddb
Author: enridaga <en...@apache.org>
Authored: Wed Jan 15 03:09:49 2014 +0000
Committer: enridaga <en...@apache.org>
Committed: Wed Jan 15 03:09:49 2014 +0000
----------------------------------------------------------------------
.../rdf/virtuoso/storage/VirtuosoGraph.java | 6 +-
.../rdf/virtuoso/storage/VirtuosoMGraph.java | 278 ++++++++++----
.../access/VirtuosoWeightedProvider.java | 360 ++++++++++++-------
.../rdf/virtuoso/storage/ConnectionTest.java | 4 +
.../java/rdf/virtuoso/storage/RdfIOTest.java | 9 +-
.../java/rdf/virtuoso/storage/TestUtils.java | 25 +-
.../rdf/virtuoso/storage/ThreadSafetyTest.java | 2 +-
.../virtuoso/storage/VirtuosoMGraphTest.java | 2 +-
.../access/VirtuosoWeightedProviderTest.java | 12 +-
9 files changed, 481 insertions(+), 217 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoGraph.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoGraph.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoGraph.java
index dac4469..9f9a022 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoGraph.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoGraph.java
@@ -25,6 +25,8 @@ import org.apache.clerezza.rdf.core.Triple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
+
import virtuoso.jdbc4.VirtuosoConnection;
/**
* This is a read-only version of {@link VirtuosoMGraph}
@@ -39,8 +41,8 @@ public class VirtuosoGraph extends VirtuosoMGraph implements Graph{
protected Logger logger = LoggerFactory
.getLogger(VirtuosoGraph.class);
- public VirtuosoGraph(String name, VirtuosoConnection connection) {
- super(name, connection);
+ public VirtuosoGraph(String name, VirtuosoWeightedProvider provider) {
+ super(name, provider);
}
@Override
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
index 6b084ec..9b6641f 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
@@ -20,7 +20,9 @@ package rdf.virtuoso.storage;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -45,6 +47,7 @@ import org.slf4j.LoggerFactory;
import org.wymiwyg.commons.util.collections.BidiMap;
import org.wymiwyg.commons.util.collections.BidiMapImpl;
+import rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
import virtuoso.jdbc4.VirtuosoConnection;
import virtuoso.jdbc4.VirtuosoException;
import virtuoso.jdbc4.VirtuosoExtendedString;
@@ -78,7 +81,7 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
/**
* Connection
*/
- private VirtuosoConnection connection = null;
+// private VirtuosoConnection connection = null;
/**
* The name of the graph
*/
@@ -86,6 +89,7 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
private int size = 0;
private VirtuosoGraph readOnly = null;
+ private VirtuosoWeightedProvider provider = null;
/**
* Creates a {@link VirtuosoMGraph} Virtuoso MGraph binds directly to the
@@ -93,11 +97,11 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
*
* @param connection
*/
- public VirtuosoMGraph(String name, VirtuosoConnection connection) {
- logger.debug("VirtuosoMGraph(String {}, VirtuosoConnection {})", name,
- connection);
+ public VirtuosoMGraph(String name, VirtuosoWeightedProvider provider) {
+ logger.debug("VirtuosoMGraph(String {}, VirtuosoWeightedProvider {})", name,
+ provider);
this.name = name;
- this.connection = connection;
+ this.provider = provider;
this.bnodesMap = new BidiMapImpl<String, BNode>();
}
@@ -105,10 +109,12 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
* Gets the connection
*
* @return
+ * @throws ClassNotFoundException
+ * @throws SQLException
*/
- protected VirtuosoConnection getConnection() {
+ protected VirtuosoConnection getConnection() throws SQLException, ClassNotFoundException {
logger.debug("getConnection()");
- return this.connection;
+ return this.provider.getConnection();
}
@Override
@@ -127,7 +133,7 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
logger.debug("asVirtuosoGraph()");
if (this.readOnly == null) {
logger.debug("create embedded singleton read-only instance");
- this.readOnly = new VirtuosoGraph(name, connection);
+ this.readOnly = new VirtuosoGraph(name, provider);
}
return readOnly;
}
@@ -199,14 +205,25 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
String sql = sb.toString();
logger.debug("Executing SQL: {}", sql);
- Statement st;
+ Statement st = null;
+ List<Triple> list = null;
+ Exception e = null;
+ VirtuosoConnection connection = null;
+ VirtuosoResultSet rs = null;
try {
readLock.lock();
+ connection = provider.getConnection();
st = connection.createStatement();
boolean has = st.execute(sql);
- final VirtuosoResultSet rs = (VirtuosoResultSet) st.getResultSet();
+ rs = (VirtuosoResultSet) st.getResultSet();
+ list = new ArrayList<Triple>();
+ while(rs.next()){
+ list.add(new TripleBuilder(rs.getObject(1),
+ rs.getObject(2), rs.getObject(3)).build());
+ }
readLock.unlock();
- return new Iterator<Triple>() {
+ /*
+ iterator = new Iterator<Triple>() {
Triple current = null;
private boolean didNext = false;
private boolean hasNext = false;
@@ -219,13 +236,23 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
hasNext = rs.next();
didNext = true;
}
- return hasNext;
} catch (SQLException e) {
logger.error("Error while iterating results", e);
- return false;
} finally {
readLock.unlock();
}
+ if(hasNext == false){
+ // here we close the connection
+ // XXX Surely this is wrong
+ try { if (rs != null) rs.close(); } catch (Exception ex) {};
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ return hasNext;
}
@Override
@@ -259,17 +286,33 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
writeLock.unlock();
}
}
-
};
- } catch (VirtuosoException e) {
- logger.error("ERROR while executing statement", e);
+ */
+ } catch (VirtuosoException ve) {
+ logger.error("ERROR while executing statement", ve);
logger.error(" executing SQL: {}", sql);
- throw new RuntimeException(e);
- } catch (SQLException e) {
- logger.error("ERROR while executing statement", e);
+ e = ve;
+ } catch (SQLException e1) {
+ logger.error("ERROR while executing statement", e1);
logger.error(" executing SQL: {}", sql);
+ e = e1;
+ } catch (ClassNotFoundException e1) {
+ logger.error("ERROR while executing statement", e1);
+ e = e1;
+ } finally{
+ try { if (rs != null) rs.close(); } catch (Exception ex) {};
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+
+ if(list == null || e != null){
throw new RuntimeException(e);
}
+ return list.iterator();
}
/**
@@ -287,25 +330,42 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
logger.debug("clear()");
String SQL = "SPARQL CLEAR GRAPH <" + this.getName() + ">";
this.writeLock.lock();
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ Statement st =null;
try {
logger.debug("Executing SQL: {}", SQL);
- Statement st = getConnection().createStatement();
+ connection = getConnection();
+ st = connection.createStatement();
boolean success = st.execute(SQL);
if (success) {
this.size = 0;
} else {
- throw new RuntimeException(
+ e = new RuntimeException(
"Problems on clear() method. Cannot clear the graph!");
}
- } catch (VirtuosoException e) {
+ } catch (VirtuosoException ve) {
logger.error("ERROR while executing statement", e);
- throw new RuntimeException(e);
- } catch (SQLException e) {
+ e = ve;
+ } catch (SQLException se) {
logger.error("ERROR while executing statement", e);
- throw new RuntimeException(e);
+ e = se;
+ } catch (ClassNotFoundException e1) {
+ e = e1;
} finally {
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection != null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
this.writeLock.unlock();
}
+ if(e!=null){
+ throw new RuntimeException(e);
+ }
}
private void loadSize() {
@@ -313,85 +373,171 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
String SQL = "SPARQL SELECT COUNT(*) FROM <" + this.getName()
+ "> WHERE { ?s ?p ?o } ";
int size = 0;
+ logger.debug("loadSize() pre lock");
this.readLock.lock();
+ logger.debug("loadSize() post lock");
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ Statement st = null;
+ VirtuosoResultSet rs = null;
try {
logger.debug("Executing SQL: {}", SQL);
- Statement st = getConnection().createStatement();
- VirtuosoResultSet rs = (VirtuosoResultSet) st.executeQuery(SQL);
+ connection = getConnection();
+ st = connection.createStatement();
+ rs = (VirtuosoResultSet) st.executeQuery(SQL);
rs.next();
size = rs.getInt(1);
- } catch (VirtuosoException e) {
+ } catch (VirtuosoException ve) {
logger.error("ERROR while executing statement", e);
- throw new RuntimeException(e);
- } catch (SQLException e) {
+ e = ve;
+ } catch (SQLException se) {
logger.error("ERROR while executing statement", e);
- throw new RuntimeException(e);
+ e = se;
+ } catch (ClassNotFoundException e1) {
+ e = e1;
} finally {
this.readLock.unlock();
+ logger.debug("loadSize() unlock");
+ try { if (rs != null) rs.close(); } catch (Exception ex) {};
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection != null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ }
+ if(e!=null){
+ throw new RuntimeException(e);
}
this.size = size;
}
protected boolean performAdd(Triple triple) {
- return add(triple, connection);
- }
-
- protected boolean performRemove(Triple triple) {
- return remove(triple, connection);
- }
-
- /**
- * Adds a triple in the store
- *
- * @param triple
- * @param connection
- * @return
- */
- private boolean add(Triple triple, VirtuosoConnection connection) {
- logger.debug("add(Triple {}, VirtuosoConnection {})", triple,
- connection);
+ logger.debug("performAdd(Triple {})", triple);
String sql = getAddSQLStatement(triple);
logger.debug("Executing SQL: {}", sql);
writeLock.lock();
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ Statement st = null;
try {
- Statement st = connection.createStatement();
+ connection = getConnection();
+ st = connection.createStatement();
st.execute(sql);
- } catch (VirtuosoException e) {
+ } catch (VirtuosoException ve) {
logger.error("ERROR while executing statement", e);
- return false;
- } catch (SQLException e) {
+ e = ve;
+ } catch (SQLException se) {
logger.error("ERROR while executing statement", e);
- return false;
+ e = se;
+ } catch (ClassNotFoundException e1) {
+ e = e1;
} finally {
writeLock.unlock();
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection != null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ }
+ if(e!=null){
+ return false;
}
return true;
}
- /**
- * Removes a triple from the store.
- *
- * @param triple
- * @param connection
- * @return
- */
- private boolean remove(Triple triple, VirtuosoConnection connection) {
- logger.debug("remove(Triple triple, VirtuosoConnection connection)",
- triple, connection);
+ protected boolean performRemove(Triple triple) {
+ logger.debug("performRemove(Triple triple)",
+ triple);
String sql = getRemoveSQLStatement(triple);
logger.debug("Executing SQL: {}", sql);
writeLock.lock();
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ Statement st = null;
try {
- Statement st = connection.createStatement();
+ connection = getConnection();
+ st = connection.createStatement();
st.execute(sql);
- } catch (SQLException e) {
+ } catch (SQLException se) {
logger.error("ERROR while executing statement", e);
- return false;
+ e = se;
+ } catch (ClassNotFoundException e1) {
+ e = e1;
} finally {
writeLock.unlock();
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection != null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ }
+ if(e!=null){
+ return false;
}
return true;
}
+//
+// /**
+// * Adds a triple in the store
+// *
+// * @param triple
+// * @param connection
+// * @return
+// */
+// private boolean add(Triple triple, VirtuosoConnection connection) {
+// logger.debug("add(Triple {}, VirtuosoConnection {})", triple,
+// connection);
+// String sql = getAddSQLStatement(triple);
+// logger.debug("Executing SQL: {}", sql);
+// writeLock.lock();
+// try {
+// Statement st = connection.createStatement();
+// st.execute(sql);
+// } catch (VirtuosoException e) {
+// logger.error("ERROR while executing statement", e);
+// return false;
+// } catch (SQLException e) {
+// logger.error("ERROR while executing statement", e);
+// return false;
+// } finally {
+// writeLock.unlock();
+// }
+// return true;
+// }
+
+// /**
+// * Removes a triple from the store.
+// *
+// * @param triple
+// * @param connection
+// * @return
+// */
+// private boolean remove(Triple triple, VirtuosoConnection connection) {
+// logger.debug("remove(Triple triple, VirtuosoConnection connection)",
+// triple, connection);
+// String sql = getRemoveSQLStatement(triple);
+// logger.debug("Executing SQL: {}", sql);
+// writeLock.lock();
+// try {
+// Statement st = connection.createStatement();
+// st.execute(sql);
+// } catch (SQLException e) {
+// logger.error("ERROR while executing statement", e);
+// return false;
+// } finally {
+// writeLock.unlock();
+// }
+// return true;
+// }
/**
* Returns the graph name
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
index f82421c..e95972f 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
@@ -18,6 +18,7 @@
*/
package rdf.virtuoso.storage.access;
+import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
@@ -102,7 +103,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
/**
* JDBC Connection to Virtuoso DBMS
*/
- private VirtuosoConnection connection = null;
+// private VirtuosoConnection connection = null;
/**
* Weight
@@ -114,6 +115,11 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
*/
private Logger logger = LoggerFactory
.getLogger(VirtuosoWeightedProvider.class);
+ private String host;
+ private Integer port;
+ private String user;
+ private String pwd;
+ private String connStr;
/**
* Creates a new {@link VirtuosoWeightedProvider}.
@@ -125,29 +131,36 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
logger.debug("Created VirtuosoWeightedProvider.");
}
- /**
- * Creates a new {@link VirtuosoWeightedProvider}
- *
- * @param connection
- */
- public VirtuosoWeightedProvider(VirtuosoConnection connection) {
- logger.debug("Created VirtuosoWeightedProvider with connection: {}",
- connection);
- this.connection = connection;
- }
-
- /**
- * Creates a new {@link VirtuosoWeightedProvider}
- *
- * @param connection
- * @param weight
- */
- public VirtuosoWeightedProvider(VirtuosoConnection connection, int weight) {
- logger.debug(
- "Created VirtuosoWeightedProvider with connection = {} and weight = {}.",
- connection, weight);
- this.weight = weight;
- this.connection = connection;
+// /**
+// * Creates a new {@link VirtuosoWeightedProvider}
+// *
+// * @param connection
+// */
+// public VirtuosoWeightedProvider(VirtuosoConnection connection) {
+// logger.debug("Created VirtuosoWeightedProvider with connection: {}",
+// connection);
+//// this.connection = connection;
+// }
+
+// /**
+// * Creates a new {@link VirtuosoWeightedProvider}
+// *
+// * @param connection
+// * @param weight
+// */
+// public VirtuosoWeightedProvider(VirtuosoConnection connection, int weight) {
+// logger.debug(
+// "Created VirtuosoWeightedProvider with connection = {} and weight = {}.",
+// connection, weight);
+// this.weight = weight;
+//// this.connection = connection;
+// }
+
+ public VirtuosoWeightedProvider(String jdbcConnectionString,
+ String jdbcUser, String jdbcPassword) {
+ connStr = jdbcConnectionString;
+ user = jdbcUser;
+ pwd = jdbcPassword;
}
/**
@@ -164,8 +177,9 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
public void activate(ComponentContext cCtx) {
logger.info("activate(ComponentContext {})", cCtx);
logger.info("Activating VirtuosoWeightedProvider...");
- if (cCtx == null && connection == null) {
- logger.error("No component context given and connection was not set");
+// if (cCtx == null && connection == null) {
+ if (cCtx == null ) {
+ logger.error("No component context given and connection was not set");
throw new IllegalArgumentException(
"No component context given and connection was not set");
} else if (cCtx != null) {
@@ -185,17 +199,19 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
/**
* Retrieve connection properties
*/
- String host = (String) cCtx.getProperties().get(HOST);
- int port = (Integer) cCtx.getProperties().get(PORT);
- String user = (String) cCtx.getProperties().get(USER);
- String pwd = (String) cCtx.getProperties().get(PASSWORD);
+ host = (String) cCtx.getProperties().get(HOST);
+ port = (Integer) cCtx.getProperties().get(PORT);
+ user = (String) cCtx.getProperties().get(USER);
+ pwd = (String) cCtx.getProperties().get(PASSWORD);
// Build connection string
- String connStr = new StringBuilder().append("jdbc:virtuoso://")
+ connStr = new StringBuilder().append("jdbc:virtuoso://")
.append(host).append(":").append(port).toString();
+
// Init connection
- this.initConnection(connStr, user, pwd);
-
+// this.initConnection(connStr, user, pwd);
+ VirtuosoConnection connection = getConnection(connStr, user, pwd);
+
// Debug activation
if (logger.isDebugEnabled()) {
logger.debug("Component context properties: ");
@@ -223,6 +239,9 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
}
logger.info("Connection to {} initialized. User is {}",
connStr, user);
+
+ // everything went ok
+ connection.close();
} catch (VirtuosoException e) {
logger.error(
"A problem occurred while intializing connection to Virtuoso",
@@ -258,20 +277,20 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
@Deactivate
public void deactivate(ComponentContext cCtx) {
logger.debug("deactivate(ComponentContext {})", cCtx);
- try {
- if (this.connection != null) {
- if (this.connection.isClosed()) {
- logger.debug("Connection is already closed");
- } else {
- logger.debug("Closing connection");
- // We close the connection
- this.connection.close();
- }
- }
- } catch (Exception re) {
- logger.warn(re.toString(), re);
- throw new RuntimeException(re);
- }
+// try {
+// if (this.connection != null) {
+// if (this.connection.isClosed()) {
+// logger.debug("Connection is already closed");
+// } else {
+// logger.debug("Closing connection");
+// // We close the connection
+// this.connection.close();
+// }
+// }
+// } catch (Exception re) {
+// logger.warn(re.toString(), re);
+// throw new RuntimeException(re);
+// }
logger.info("Shutdown complete.");
}
@@ -284,15 +303,44 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
* @throws SQLException
* @throws ClassNotFoundException
*/
- private void initConnection(String connStr, String user, String pwd)
- throws SQLException, ClassNotFoundException {
- logger.debug("initConnection(String {}, String {}, String *******)",
+// private void initConnection(String connStr, String user, String pwd)
+// throws SQLException, ClassNotFoundException {
+// logger.debug("initConnection(String {}, String {}, String *******)",
+// connStr, user);
+// if (this.connection != null) {
+// logger.debug("Connection already instantiated: {}", this.connection);
+// logger.debug("Closing connection");
+// this.connection.close();
+// }
+// /**
+// * FIXME For some reasons, it looks the DriverManager is instantiating a
+// * new virtuoso.jdbc4.Driver instance upon any activation. (Enable debug
+// * to see this in the stderr stream)
+// */
+// logger.debug("Loading JDBC Driver");
+// Class.forName(VirtuosoWeightedProvider.DRIVER, true, this.getClass()
+// .getClassLoader());
+// if (logger.isDebugEnabled()) {
+// logger.debug("Activating logging for DriverManager in stderr");
+// // FIXME! How to redirect logging to our logger???
+// DriverManager.setLogWriter(new PrintWriter(System.err));
+// }
+// connection = (VirtuosoConnection) DriverManager.getConnection(connStr,
+// user, pwd);
+// logger.debug("Connection initialized: {}", connection);
+// }
+ public VirtuosoConnection getConnection() throws SQLException, ClassNotFoundException{
+ return getConnection(connStr, user, pwd);
+ }
+ private VirtuosoConnection getConnection(String connStr, String user, String pwd)
+ throws SQLException, ClassNotFoundException {
+ logger.debug("getConnection(String {}, String {}, String *******)",
connStr, user);
- if (this.connection != null) {
- logger.debug("Connection already instantiated: {}", this.connection);
- logger.debug("Closing connection");
- this.connection.close();
- }
+ // if (this.connection != null) {
+ // logger.debug("Connection already instantiated: {}", this.connection);
+ // logger.debug("Closing connection");
+ // this.connection.close();
+ // }
/**
* FIXME For some reasons, it looks the DriverManager is instantiating a
* new virtuoso.jdbc4.Driver instance upon any activation. (Enable debug
@@ -306,9 +354,8 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
// FIXME! How to redirect logging to our logger???
DriverManager.setLogWriter(new PrintWriter(System.err));
}
- connection = (VirtuosoConnection) DriverManager.getConnection(connStr,
- user, pwd);
- logger.debug("Connection initialized: {}", connection);
+ return (VirtuosoConnection) DriverManager.getConnection(connStr, user,
+ pwd);
}
/**
@@ -316,22 +363,23 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
*
* @return
*/
- public boolean isConnectionAlive() {
- logger.debug("isConnectionAlive() : {}", connection);
- if (this.connection == null) {
- logger.warn("Connection is null");
- return false;
- }
- if (this.connection.isClosed()) {
- logger.warn("Connection is closed");
- return false;
- }
- if (this.connection.isConnectionLost()) {
- logger.warn("Connection is lost");
- return false;
- }
- return true;
- }
+// public boolean isConnectionAlive() {
+//
+// logger.debug("isConnectionAlive() : {}", connection);
+// if (this.connection == null) {
+// logger.warn("Connection is null");
+// return false;
+// }
+// if (this.connection.isClosed()) {
+// logger.warn("Connection is closed");
+// return false;
+// }
+// if (this.connection.isConnectionLost()) {
+// logger.warn("Connection is lost");
+// return false;
+// }
+// return true;
+// }
/**
* Retrieves the Graph (unmodifiable) with the given UriRef If no graph
@@ -384,6 +432,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
*/
private VirtuosoMGraph loadGraphOnce(UriRef name) {
logger.debug("loadGraphOnce({})", name);
+
// Is it the first itme we invoke a graph here?
if (graphs == null) {
graphs = new HashMap<UriRef, VirtuosoMGraph>();
@@ -392,47 +441,71 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
if (graphs.containsKey(name)) {
logger.debug("{} is already loaded", name);
return graphs.get(name);
- }
-
- logger.debug("Attempt to load {}", name);
- // Let's create the graph object
- String SQL = "SPARQL SELECT ?G WHERE { GRAPH ?G {?A ?B ?C} . FILTER(?G = "
- + name + ")} LIMIT 1";
-
- Statement st;
- try {
- st = connection.createStatement();
- logger.debug("Executing SQL: {}", SQL);
- st.execute(SQL);
- VirtuosoResultSet rs = (VirtuosoResultSet) st.getResultSet();
- if (rs.next() == false) {
- // The graph is empty, it is not readable or does not exists
- logger.warn("Graph does not exists: {}", name);
- throw new NoSuchEntityException(name);
- } else {
- // The graph exists and it is readable ...
- logger.debug("Graph {} is readable", name);
- // is it writable?
- logger.debug("Is {} writable?", name);
- if (canModify(name)) {
- logger.debug("Creating writable MGraph for graph {}", name);
- graphs.put(name, new VirtuosoMGraph(
- name.getUnicodeString(), connection));
+ }else{
+ VirtuosoMGraph graph = null;
+ logger.debug("Attempt to load {}", name);
+ // Let's create the graph object
+ String SQL = "SPARQL SELECT ?G WHERE { GRAPH ?G {?A ?B ?C} . FILTER(?G = "
+ + name + ")} LIMIT 1";
+
+ Statement st = null;
+ VirtuosoResultSet rs = null;
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ try {
+ connection = getConnection(connStr, user, pwd);
+ st = connection.createStatement();
+ logger.debug("Executing SQL: {}", SQL);
+ st.execute(SQL);
+ rs = (VirtuosoResultSet) st.getResultSet();
+ if (rs.next() == false) {
+ // The graph is empty, it is not readable or does not exists
+ logger.warn("Graph does not exists: {}", name);
+ throw new NoSuchEntityException(name);
} else {
- logger.debug("Creating read-only Graph for graph {}", name);
- graphs.put(name, new VirtuosoMGraph(
- name.getUnicodeString(), connection)
- .asVirtuosoGraph());
+ // The graph exists and it is readable ...
+ logger.debug("Graph {} is readable", name);
+ // is it writable?
+ logger.debug("Is {} writable?", name);
+ if (canModify(name)) {
+ logger.debug("Creating writable MGraph for graph {}", name);
+ graphs.put(name, new VirtuosoMGraph(
+ name.getUnicodeString(), this));
+ } else {
+ logger.debug("Creating read-only Graph for graph {}", name);
+ graphs.put(name, new VirtuosoMGraph(
+ name.getUnicodeString(), this)
+ .asVirtuosoGraph());
+ }
+ graph = graphs.get(name);
+ }
+
+ } catch (VirtuosoException ve) {
+ logger.error("Error while executing query/connection.", ve);
+ e = ve;
+ } catch (SQLException se) {
+ logger.error("Error while executing query/connection.", se);
+ e = se;
+ } catch (ClassNotFoundException ce) {
+ logger.error("Error while executing query/connection.", ce);
+ e = ce;
+ }finally{
+ try { if (rs != null) rs.close(); } catch (Exception ex) {};
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection!=null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
}
- return graphs.get(name);
}
- } catch (VirtuosoException e) {
- logger.error("Error while executing query/connection.", e);
- throw new RuntimeException(e);
- } catch (SQLException e) {
- logger.error("Error while executing query/connection.", e);
- throw new RuntimeException(e);
+ if(e!=null){
+ throw new RuntimeException(e);
+ }
+ return graph;
}
+
}
/**
@@ -459,18 +532,43 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
Set<UriRef> graphs = new HashSet<UriRef>();
// Returns the list of graphs in the virtuoso quad store
String SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {?S ?P ?O} }";
+ VirtuosoConnection connection = null;
+ Exception e = null;
+ VirtuosoStatement st = null;
+ VirtuosoResultSet rs = null;
try {
- VirtuosoStatement st = (VirtuosoStatement) this.connection
+ connection = getConnection();
+ st = (VirtuosoStatement) connection
.createStatement();
logger.debug("Executing SQL: {}", SQL);
- VirtuosoResultSet rs = (VirtuosoResultSet) st.executeQuery(SQL);
+ rs = (VirtuosoResultSet) st.executeQuery(SQL);
while (rs.next()) {
UriRef graph = new UriRef(rs.getString(1));
logger.debug(" > Graph {}", graph);
graphs.add(graph);
}
- } catch (VirtuosoException e) {
- logger.error("Error while executing query/connection.", e);
+ } catch (VirtuosoException e1) {
+ logger.error("Error while executing query/connection.", e1);
+ e = e1;
+ } catch (SQLException e1) {
+ logger.error("Error while executing query/connection.", e1);
+ e = e1;
+ } catch (ClassNotFoundException e1) {
+ logger.error("Error while executing query/connection.", e1);
+ e = e1;
+ }finally{
+
+ try { if (rs != null) rs.close(); } catch (Exception ex) {};
+ try { if (st != null) st.close(); } catch (Exception ex) {};
+ if(connection!=null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ }
+ if(e!=null){
throw new RuntimeException(e);
}
return Collections.unmodifiableSet(graphs);
@@ -492,8 +590,12 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
}
private long getPermissions(String graph) {
+ VirtuosoConnection connection = null;
+ logger.debug("getPermissions(String {})", graph);
+ Exception e = null;
+ Long result = null;
try {
- logger.debug("getPermissions(String {})", graph);
+ connection = getConnection();
ResultSet rs;
String sql = "SELECT DB.DBA.RDF_GRAPH_USER_PERMS_GET ('" + graph
+ "','" + connection.getMetaData().getUserName() + "') ";
@@ -502,16 +604,30 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
st.execute(sql);
rs = st.getResultSet();
rs.next();
- long result = rs.getLong(1);
+ result = rs.getLong(1);
logger.debug("Permission: {}", result);
- return result;
- } catch (VirtuosoException e) {
+ } catch (VirtuosoException ve) {
logger.error("A virtuoso SQL exception occurred.");
- throw new RuntimeException(e);
- } catch (SQLException e) {
+ e = ve;
+ } catch (SQLException se) {
logger.error("An SQL exception occurred.");
+ e = se;
+ } catch (ClassNotFoundException e1) {
+ logger.error("An ClassNotFoundException occurred.");
+ e = e1;
+ } finally {
+ if(connection!=null){
+ try {
+ connection.close();
+ } catch (VirtuosoException e1) {
+ logger.error("Cannot close connection", e1);
+ }
+ }
+ }
+ if (e != null) {
throw new RuntimeException(e);
}
+ return result;
}
public boolean canRead(UriRef graph) {
@@ -556,7 +672,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
} catch (NoSuchEntityException nsee) {
if (canModify(name)) {
graphs.put(name, new VirtuosoMGraph(name.getUnicodeString(),
- connection));
+ this));
return graphs.get(name);
} else {
logger.error("Cannot create MGraph {}", name);
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
index 0b5cb9c..40bd50c 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ConnectionTest.java
@@ -49,6 +49,10 @@ public class ConnectionTest {
@BeforeClass
public static void before() throws ClassNotFoundException, SQLException {
+ if (TestUtils.SKIP) {
+ log.warn("SKIPPED");
+ return;
+ }
connection = TestUtils.getConnection();
}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
index 71ada50..0db7a88 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/RdfIOTest.java
@@ -36,6 +36,8 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
+
import virtuoso.jdbc4.VirtuosoException;
public class RdfIOTest {
@@ -43,7 +45,7 @@ public class RdfIOTest {
static final String TEST_GRAPH_NAME = "RdfIOTest";
static final String XSD = "http://www.w3.org/2001/XMLSchema#";
static Logger log = LoggerFactory.getLogger(RdfIOTest.class);
-
+ static VirtuosoWeightedProvider wp ;
/**
* Clean before any test
*
@@ -56,7 +58,8 @@ public class RdfIOTest {
log.warn("SKIPPED");
return;
}
- mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getConnection());
+ wp = TestUtils.getProvider();
+ mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, wp);
mgraph.clear();
log.debug("Clearing graph <{}>", TEST_GRAPH_NAME);
}
@@ -127,7 +130,7 @@ public class RdfIOTest {
}
private Triple writeAndRead(NonLiteral subject, UriRef predicate, Resource object) throws ClassNotFoundException, SQLException{
- VirtuosoMGraph graph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getConnection());
+ VirtuosoMGraph graph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getProvider());
Triple t = new TripleImpl(subject, predicate, object);
graph.add(t);
Triple read = graph.getGraph().iterator().next();
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
index d08ad23..993bcee 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/TestUtils.java
@@ -43,7 +43,7 @@ public class TestUtils {
public static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
- private static VirtuosoConnection connection = null;
+ private static VirtuosoWeightedProvider provider = null;
private static String jdbcConnectionString = null;
private static String jdbcUser = null;
private static String jdbcPassword = null;
@@ -60,15 +60,18 @@ public class TestUtils {
SKIP = !skipProperty.equals("true");
}
- public static VirtuosoConnection getConnection()
+ public static VirtuosoConnection getConnection() throws SQLException, ClassNotFoundException{
+ return getProvider().getConnection();
+ }
+ public static VirtuosoWeightedProvider getProvider()
throws ClassNotFoundException, SQLException {
- if (connection == null) {
- initConnection();
+ if (provider == null) {
+ initProvider();
}
- return connection;
+ return provider;
}
- private static void initConnection() throws ClassNotFoundException,
+ private static void initProvider() throws ClassNotFoundException,
SQLException {
if (SKIP) {
log.warn("SKIPPING ALL TESTS!");
@@ -108,11 +111,11 @@ public class TestUtils {
cb.append(":");
cb.append(port);
jdbcConnectionString = cb.toString();
- Class.forName(VirtuosoWeightedProvider.DRIVER);
- log.info("Create connection");
- connection = (VirtuosoConnection) DriverManager.getConnection(
- jdbcConnectionString, jdbcUser, jdbcPassword);
-
+// Class.forName(VirtuosoWeightedProvider.DRIVER);
+ log.info("Create provider");
+// connection = (VirtuosoConnection) DriverManager.getConnection(
+// jdbcConnectionString, jdbcUser, jdbcPassword);
+ provider = new VirtuosoWeightedProvider(jdbcConnectionString, jdbcUser, jdbcPassword);
log.debug("Connection URL: {}", jdbcConnectionString);
log.debug("Connection user: {}", jdbcUser);
}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
index 35ac95e..966da2c 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/ThreadSafetyTest.java
@@ -61,7 +61,7 @@ public class ThreadSafetyTest {
return;
}
mgraph = new VirtuosoMGraph("ThreadSafetyTest",
- TestUtils.getConnection());
+ TestUtils.getProvider());
executor = Executors.newCachedThreadPool();
}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoMGraphTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoMGraphTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoMGraphTest.java
index 598884a..a597c78 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoMGraphTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/VirtuosoMGraphTest.java
@@ -62,7 +62,7 @@ public class VirtuosoMGraphTest {
log.warn("SKIPPED");
return;
}
- mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getConnection());
+ mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, TestUtils.getProvider());
mgraph.clear();
log.debug("Clearing graph <{}>", TEST_GRAPH_NAME);
}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/5a2fdceb/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
index 2314fcc..5757049 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/rdf/virtuoso/storage/access/VirtuosoWeightedProviderTest.java
@@ -67,17 +67,7 @@ public class VirtuosoWeightedProviderTest {
log.warn("SKIPPED");
return;
}
- wp = new VirtuosoWeightedProvider(TestUtils.getConnection());
- }
-
- @Test
- public void initialization() throws ClassNotFoundException, SQLException {
- log.info("Check if initialized correctly with wp.isConnectionAlive()");
- if (TestUtils.SKIP) {
- log.warn("SKIPPED");
- return;
- }
- assertTrue(wp.isConnectionAlive());
+ wp = TestUtils.getProvider();
}
@Test