Skip to content

Instantly share code, notes, and snippets.

@StephanSchrader
Last active August 29, 2015 14:23
Show Gist options
  • Save StephanSchrader/a26f2f80d876e9d052f6 to your computer and use it in GitHub Desktop.
Save StephanSchrader/a26f2f80d876e9d052f6 to your computer and use it in GitHub Desktop.
OpenCms container managed DB connections
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org>
*/
package de.mustnotbenamed.opencms.db.jndi;
import org.apache.commons.logging.Log;
import org.opencms.main.CmsLog;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Locale;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
* @author schrader
*/
public class CmsSqlManager extends org.opencms.db.generic.CmsSqlManager {
private static final Log LOG = CmsLog.getLog(CmsSqlManager.class.getName());
private DataSource dataSource;
@Override
public void init(int driverType, String poolUrl) {
super.init(driverType, poolUrl);
}
protected DataSource getDataSource(String jndiName) {
// TODO do lookup for an dataSource with given jndiName. Which can be configured in:
// opencms.properties, key: db.[???].pool
if (dataSource != null) {
return dataSource;
}
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/OpenCmsDb");
} catch (NamingException e) {
throw new RuntimeException(e.getMessage(), e);
}
try (Connection con = dataSource.getConnection()) {
DatabaseMetaData metadata = con.getMetaData();
String databaseProductName = metadata.getDatabaseProductName();
LOG.info("Connection successful, db: '" + databaseProductName + "'");
loadQueryProperties(
String.format("org/opencms/db/%s/query.properties", databaseProductName.toLowerCase(
Locale.ROOT)));
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
return dataSource;
}
@Override
public Connection getConnectionByUrl(String dbPoolUrl) throws SQLException {
LOG.debug(
String.format("Returns connection from default dataSource, dbPoolUrl: '%s'", dbPoolUrl));
return getDataSource("default").getConnection();
}
@Override
public Connection getConnection(String dbPoolName) throws SQLException {
LOG.debug(
String.format("Returns connection from default dataSource, dbPoolName: '%s'", dbPoolName));
return getDataSource(dbPoolName).getConnection();
}
}
## Configure DataSource in Tomcat (server.xml)
<Resource name="jdbc/OpenCmsDb" auth="Container"
type="javax.sql.DataSource" username="opencms" password="opencms"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/opencms"
maxActive="8"
/>
## Configure Driver in OpenCms (opencms.properties)
replace
db.[???].sqlmanager=org.opencms.db.postgresql.CmsSqlManager
with
db.[???].sqlmanager=de.mustnotbenamed.opencms.db.jndi.CmsSqlManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment