From: markt Date: Fri, 3 Jun 2011 22:13:09 +0000 (+0000) Subject: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51264 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=01fa36bf666a5f07ae1de4bbaf6e03120799a9eb;p=tomcat7.0 Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51264 Allow the JDBC persistent session store to use a JNDI datasource to define the database in which sessions are persisted. Patch provided by Felix Schumacher. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1131263 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/session/JDBCStore.java b/java/org/apache/catalina/session/JDBCStore.java index 8a8943e15..7580c2b5a 100644 --- a/java/org/apache/catalina/session/JDBCStore.java +++ b/java/org/apache/catalina/session/JDBCStore.java @@ -33,6 +33,11 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Properties; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + import org.apache.catalina.Container; import org.apache.catalina.LifecycleException; import org.apache.catalina.Loader; @@ -102,6 +107,16 @@ public class JDBCStore extends StoreBase { */ protected String driverName = null; + /** + * name of the JNDI resource + */ + protected String dataSourceName = null; + + /** + * DataSource to use + */ + protected DataSource dataSource = null; + // ------------------------------------------------------------- Table & cols /** @@ -436,6 +451,27 @@ public class JDBCStore extends StoreBase { return (this.sessionLastAccessedCol); } + /** + * Set the JNDI name of a DataSource-factory to use for db access + * + * @param dataSourceName The JNDI name of the DataSource-factory + */ + public void setDataSourceName(String dataSourceName) { + if (dataSourceName == null || "".equals(dataSourceName.trim())) { + manager.getContainer().getLogger().warn( + sm.getString(getStoreName() + ".missingDataSourceName")); + return; + } + this.dataSourceName = dataSourceName; + } + + /** + * Return the name of the JNDI DataSource-factory + */ + public String getDataSourceName() { + return this.dataSourceName; + } + // --------------------------------------------------------- Public Methods /** @@ -866,6 +902,24 @@ public class JDBCStore extends StoreBase { if (dbConnection != null) return (dbConnection); + if (dataSourceName != null && dataSource == null) { + Context initCtx; + try { + initCtx = new InitialContext(); + Context envCtx = (Context) initCtx.lookup("java:comp/env"); + this.dataSource = (DataSource) envCtx.lookup(this.dataSourceName); + } catch (NamingException e) { + manager.getContainer().getLogger().error( + sm.getString(getStoreName() + ".wrongDataSource", + this.dataSourceName), e); + } + } + + if (dataSource != null) { + dbConnection = dataSource.getConnection(); + return dbConnection; + } + // Instantiate our database driver if necessary if (driver == null) { try { diff --git a/java/org/apache/catalina/session/LocalStrings.properties b/java/org/apache/catalina/session/LocalStrings.properties index df933a846..b16f94267 100644 --- a/java/org/apache/catalina/session/LocalStrings.properties +++ b/java/org/apache/catalina/session/LocalStrings.properties @@ -27,6 +27,8 @@ JDBCStore.checkConnectionDBClosed=The database connection is null or was found t JDBCStore.checkConnectionDBReOpenFail=The re-open on the database failed. The database could be down. JDBCStore.checkConnectionSQLException=A SQL exception occurred {0} JDBCStore.checkConnectionClassNotFoundException=JDBC driver class not found {0} +JDBCStore.wrongDataSource=Can't open JNDI DataSource [{0}] +JDBCStore.missingDataSourceName=No valid JNDI name was given. managerBase.createRandom=Created random number generator for session ID generation in {0}ms. managerBase.createSession.ise=createSession: Too many active sessions managerBase.sessionTimeout=Invalid session timeout setting {0} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 1c2f30b12..31e39e2fc 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -69,6 +69,11 @@ without error. (markt) + 51264: Allow the JDBC persistent session store to use a + JNDI datasource to define the database in which sessions are persisted. + Patch provided by Felix Schumacher. (markt) + + 51274: Add missing i18n strings in PersistentManagerBase. Patch provided by Eiji Takahashi. (markt) diff --git a/webapps/docs/config/manager.xml b/webapps/docs/config/manager.xml index 01a0edcc0..f5b6bdcc5 100644 --- a/webapps/docs/config/manager.xml +++ b/webapps/docs/config/manager.xml @@ -356,6 +356,13 @@ session table.

+ +

Name of the JNDI resource for a JDBC DataSource-factory. If this option + is given and a valid JDBC resource can be found, it will be used and any + direct configuration of a JDBC connection via connectionURL + and driverName will be ignored.

+
+

Java class name of the JDBC driver to be used.