Author: tolusha
Date: 2011-07-13 07:47:50 -0400 (Wed, 13 Jul 2011)
New Revision: 4622
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/CloseableDataSource.java
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/SimpleContext.java
Log:
EXOJCR-1425: Close all database connections
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/CloseableDataSource.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/CloseableDataSource.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/CloseableDataSource.java 2011-07-13
11:47:50 UTC (rev 4622)
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc.impl;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+/**
+ * This class is used to wrap the original {@link DataSource}
+ * in order to be able to support close operation.
+ *
+ * @author <a href="abazko(a)exoplatform.com">Anatoliy Bazko</a>
+ * @version $Id$
+ */
+public class CloseableDataSource implements DataSource
+{
+ /**
+ * The wrapped {@link DataSource}
+ */
+ private DataSource ds;
+
+ /**
+ * Constructor CloseableDataSource.
+ */
+ public CloseableDataSource(DataSource ds)
+ {
+ this.ds = ds;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public PrintWriter getLogWriter() throws SQLException
+ {
+ checkValid();
+ return ds.getLogWriter();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLoginTimeout() throws SQLException
+ {
+ checkValid();
+ return ds.getLoginTimeout();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setLogWriter(PrintWriter out) throws SQLException
+ {
+ checkValid();
+ ds.setLogWriter(out);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setLoginTimeout(int seconds) throws SQLException
+ {
+ checkValid();
+ ds.setLoginTimeout(seconds);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isWrapperFor(Class<?> iface) throws SQLException
+ {
+ checkValid();
+ return ds.isWrapperFor(iface);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public <T> T unwrap(Class<T> iface) throws SQLException
+ {
+ checkValid();
+ return ds.unwrap(iface);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Connection getConnection() throws SQLException
+ {
+ checkValid();
+ return ds.getConnection();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Connection getConnection(String username, String password) throws SQLException
+ {
+ checkValid();
+ return ds.getConnection(username, password);
+ }
+
+ /**
+ * Closes datasource to release all idle connections.
+ */
+ public void close()
+ {
+ ds = null;
+ }
+
+ /**
+ * Check if datasouce already closed.
+ *
+ * @throws SQLException
+ * if datasource is closed
+ */
+ private void checkValid() throws SQLException
+ {
+ if (ds == null)
+ {
+ throw new SQLException("The datasource is closed");
+ }
+ }
+}
Property changes on:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/CloseableDataSource.java
___________________________________________________________________
Added: svn:keywords
+ Id
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/SimpleContext.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/SimpleContext.java 2011-07-13
06:10:10 UTC (rev 4621)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/SimpleContext.java 2011-07-13
11:47:50 UTC (rev 4622)
@@ -18,6 +18,7 @@
*/
package org.exoplatform.services.naming;
+import org.exoplatform.services.jdbc.impl.CloseableDataSource;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
@@ -41,6 +42,7 @@
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.spi.NamingManager;
+import javax.sql.DataSource;
/**
* Created by The eXo Platform SAS.
@@ -112,6 +114,12 @@
try
{
obj = NamingManager.getObjectInstance(obj, NAME_PARSER.parse(name),
this, getInternalEnv());
+
+ if (obj instanceof DataSource)
+ {
+ obj = new CloseableDataSource((DataSource)obj);
+ }
+
// Re-bind with the object with its new value to be able to return the
same ins
bind(name, obj, false);
}