Author: nfilotto
Date: 2009-12-14 10:30:42 -0500 (Mon, 14 Dec 2009)
New Revision: 1047
Added:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java
Log:
EXOJCR-199: JBoss Cache persistence prototype
JDBCStorageConnectionFactory has been added to hold the JDBC connection opened in order to
reduce the total amount of TimeoutException
Added:
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java
===================================================================
---
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java
(rev 0)
+++
jcr/branches/1.12.0-JBC/component/core/src/main/java/org/exoplatform/services/jcr/impl/storage/jbosscache/JDBCStorageConnectionFactory.java 2009-12-14
15:30:42 UTC (rev 1047)
@@ -0,0 +1,79 @@
+package org.exoplatform.services.jcr.impl.storage.jbosscache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+
+import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCStorageConnection;
+import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
+
+/**
+ * This class is used to holds the JDBC connection open during the live time of
+ * the {@link JBossCacheStorageConnection}. We keep one
+ * {@link JDBCStorageConnection} per JDBCStorageConnection into a
+ * {@link ThreadLocal}
+ *
+ * @author nicolasfilotto
+ *
+ */
+public class JDBCStorageConnectionFactory {
+
+ /**
+ * The {@link ThreadLocal} in which we store all the current opened
+ * connections
+ */
+ private static final ThreadLocal<Map<WorkspaceDataContainer,
JDBCStorageConnection>> CONNECTIONS = new
ThreadLocal<Map<WorkspaceDataContainer, JDBCStorageConnection>>();
+
+ /**
+ * Gives the current opened connection for the related
+ * {@link WorkspaceDataContainer} if it exists, returns a new one otherwise
+ *
+ * @param dataContainer
+ * the data container for which we want a connection
+ * @return an opened connection
+ * @throws RepositoryException
+ * if an error occurs
+ */
+ static JDBCStorageConnection get(WorkspaceDataContainer dataContainer)
+ throws RepositoryException {
+ Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS
+ .get();
+ if (connections == null) {
+ connections = new HashMap<WorkspaceDataContainer, JDBCStorageConnection>();
+ CONNECTIONS.set(connections);
+ }
+ JDBCStorageConnection connection = connections.get(dataContainer);
+ if (connection == null || !connection.isOpened()) {
+ connection = (JDBCStorageConnection) dataContainer.openConnection();
+ connections.put(dataContainer, connection);
+ }
+ return connection;
+ }
+
+ /**
+ * Close the current opened connection related to the given
+ * {@link WorkspaceDataContainer}
+ *
+ * @param dataContainer
+ * the data container for which we want to close the connection
+ * @throws IllegalStateException
+ * if connection is already closed
+ * @throws RepositoryException
+ * if some exception occured
+ */
+ static void close(WorkspaceDataContainer dataContainer)
+ throws IllegalStateException, RepositoryException {
+ Map<WorkspaceDataContainer, JDBCStorageConnection> connections = CONNECTIONS
+ .get();
+ if (connections == null) {
+ return;
+ }
+ JDBCStorageConnection connection = connections.remove(dataContainer);
+ if (connection == null || !connection.isOpened()) {
+ return;
+ }
+ connection.close();
+ connection = null;
+ }
+}