code.onehippo.org is currently readonly. We are migrating to code.bloomreach.com, please continue working there on Monday 14/12. See: https://docs.bloomreach.com/display/engineering/GitLab

Commit 03072bf6 authored by Ard Schrijvers's avatar Ard Schrijvers

REPO-1811 Support all databases and most specifically Oracle

parent 87e1cb4b
......@@ -15,6 +15,9 @@
*/
package org.onehippo.repository.lock;
import java.sql.Connection;
import java.sql.SQLException;
import javax.jcr.RepositoryException;
import javax.sql.DataSource;
......@@ -22,7 +25,9 @@ import org.apache.jackrabbit.core.util.db.ConnectionHelper;
import org.apache.jackrabbit.core.util.db.ConnectionHelperDataSourceAccessor;
import org.hippoecm.repository.jackrabbit.RepositoryImpl;
import org.onehippo.cms7.services.lock.LockManager;
import org.onehippo.repository.lock.AbstractLockManager;
import org.onehippo.repository.lock.db.DbLockManager;
import org.onehippo.repository.lock.db.OracleDbLockManager;
import org.onehippo.repository.lock.memory.MemoryLockManager;
public class LockManagerFactory {
......@@ -45,9 +50,30 @@ public class LockManagerFactory {
if (journalConnectionHelper != null) {
final DataSource dataSource = ConnectionHelperDataSourceAccessor.getDataSource(journalConnectionHelper);
String clusterNodeId = repositoryImpl.getDescriptor("jackrabbit.cluster.id");
return new DbLockManager(dataSource, clusterNodeId == null ? "default" : clusterNodeId);
String dbProductName;
try (Connection connection = dataSource.getConnection()) {
dbProductName = connection.getMetaData().getDatabaseProductName();
} catch (SQLException e) {
throw new RepositoryException("Failed to retrieve SQL Lock Manager database metadata", e);
}
switch (dbProductName) {
case "MySQL":
return new DbLockManager(dataSource, clusterNodeId == null ? "default" : clusterNodeId);
case "PostgreSQL":
return new DbLockManager(dataSource, clusterNodeId == null ? "default" : clusterNodeId);
case "Oracle":
return new OracleDbLockManager(dataSource, clusterNodeId == null ? "default" : clusterNodeId);
case "H2":
return new DbLockManager(dataSource, clusterNodeId == null ? "default" : clusterNodeId);
default:
throw new RepositoryException("Unsupported Database engine. Product name: " + dbProductName);
}
} else {
return new MemoryLockManager();
}
}
}
......@@ -29,6 +29,7 @@ import org.onehippo.cms7.services.lock.LockException;
import org.onehippo.cms7.services.lock.LockManagerException;
import org.onehippo.repository.lock.AbstractLockManager;
import org.onehippo.repository.lock.MutableLock;
import org.onehippo.repository.lock.db.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -41,8 +42,7 @@ public class DbLockManager extends AbstractLockManager {
public final static String TABLE_NAME_LOCK = "hippo_lock";
// TODO for oracle it must be NUMBER instead of BIGINT
final static String CREATE_LOCK_TABLE_STATEMENT = "CREATE TABLE %s (" +
private final static String CREATE_LOCK_TABLE_STATEMENT = "CREATE TABLE %s (" +
"lockKey VARCHAR(256) NOT NULL, " +
"lockOwner VARCHAR(256), " +
"lockThread VARCHAR(256)," +
......@@ -93,7 +93,7 @@ public class DbLockManager extends AbstractLockManager {
public DbLockManager(final DataSource dataSource, final String clusterNodeId) {
this.dataSource = dataSource;
this.clusterNodeId = clusterNodeId;
DbHelper.createTableIfNeeded(dataSource, CREATE_LOCK_TABLE_STATEMENT, TABLE_NAME_LOCK, "lockKey");
org.onehippo.repository.lock.db.DbHelper.createTableIfNeeded(dataSource, getCreateLockTableStatement(), TABLE_NAME_LOCK, "lockKey");
addJob(new UnlockStoppedThreadJanitor());
addJob(new DbResetExpiredLocksJanitor(dataSource));
......@@ -103,6 +103,10 @@ public class DbLockManager extends AbstractLockManager {
addJob(new LockThreadInterrupter(dataSource, clusterNodeId, this));
}
protected String getCreateLockTableStatement() {
return CREATE_LOCK_TABLE_STATEMENT;
}
@Override
protected Logger getLogger() {
return log;
......
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.repository.lock.db;
import javax.sql.DataSource;
public class OracleDbLockManager extends DbLockManager {
private final static String ORACLE_CREATE_LOCK_TABLE_STATEMENT = "CREATE TABLE %s (" +
"lockKey VARCHAR(256) NOT NULL, " +
"lockOwner VARCHAR(256), " +
"lockThread VARCHAR(256)," +
"status VARCHAR(256) NOT NULL," +
"lockTime NUMBER(19)," +
"expirationTime NUMBER(19)," +
"lastModified NUMBER(19)" +
")";
public OracleDbLockManager(final DataSource dataSource, final String clusterNodeId) {
super(dataSource, clusterNodeId);
}
@Override
protected String getCreateLockTableStatement() {
return ORACLE_CREATE_LOCK_TABLE_STATEMENT;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment