Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (4.3/Cloud)

71584: Merged V4.2-BUG-FIX (4.2.3) to HEAD-BUG-FIX (4.3/Cloud)
      70325: Merged V4.1-BUG-FIX (4.1.9) to V4.2-BUG-FIX (4.2.3)
         70145: MNT-8629: Merged DEV to V4.1-BUG-FIX (4.1.9)
            70077: MNT-8629: Support requested for DB2 when schema name does not match DB user name.
               - Take into account schema name during update


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@74685 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Will Abson
2014-06-25 15:09:31 +00:00
parent 18736883bf
commit 8a2b11108a
4 changed files with 86 additions and 40 deletions

View File

@@ -49,11 +49,20 @@
</bean> </bean>
<bean id="databaseMetaDataHelper" class="org.alfresco.util.DatabaseMetaDataHelper">
<property name="localSessionFactory">
<ref bean="&amp;sessionFactory"></ref> <!-- inject the actual factory, not a session -->
</property>
</bean>
<!-- ensure that the schema is bootstrapped --> <!-- ensure that the schema is bootstrapped -->
<bean id="schemaBootstrap" class="org.alfresco.repo.domain.schema.SchemaBootstrap" > <bean id="schemaBootstrap" class="org.alfresco.repo.domain.schema.SchemaBootstrap" >
<property name="dataSource"> <property name="dataSource">
<ref bean="dataSource"/> <ref bean="dataSource"/>
</property> </property>
<property name="databaseMetaDataHelper">
<ref bean="databaseMetaDataHelper" />
</property>
<property name="appliedPatchDAO"> <property name="appliedPatchDAO">
<ref bean="appliedPatchDAO"/> <ref bean="appliedPatchDAO"/>
</property> </property>

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2013 Alfresco Software Limited. * Copyright (C) 2005-2010 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -229,6 +229,17 @@ public class SchemaBootstrap extends AbstractLifecycleBean
this.descriptorService = descriptorService; this.descriptorService = descriptorService;
} }
/**
* Defines the DatabaseMetaDataHelper to be used
*
* @param databaseMetaDataHelper
*/
public void setDatabaseMetaDataHelper(DatabaseMetaDataHelper databaseMetaDataHelper)
{
this.databaseMetaDataHelper = databaseMetaDataHelper;
}
/** /**
* Sets the previously auto-detected Hibernate dialect. * Sets the previously auto-detected Hibernate dialect.
* *
@@ -260,6 +271,7 @@ public class SchemaBootstrap extends AbstractLifecycleBean
private int maximumStringLength; private int maximumStringLength;
private Properties globalProperties; private Properties globalProperties;
private String dbSchemaName; private String dbSchemaName;
private DatabaseMetaDataHelper databaseMetaDataHelper;
private ThreadLocal<StringBuilder> executedStatementsThreadLocal = new ThreadLocal<StringBuilder>(); private ThreadLocal<StringBuilder> executedStatementsThreadLocal = new ThreadLocal<StringBuilder>();
@@ -563,7 +575,7 @@ public class SchemaBootstrap extends AbstractLifecycleBean
*/ */
private int countAppliedPatches(Configuration cfg, Connection connection) throws Exception private int countAppliedPatches(Configuration cfg, Connection connection) throws Exception
{ {
String defaultSchema = dbSchemaName != null ? dbSchemaName : DatabaseMetaDataHelper.getSchema(connection); String defaultSchema = dbSchemaName != null ? dbSchemaName : databaseMetaDataHelper.getSchema(connection);
if (defaultSchema != null && defaultSchema.length() == 0) if (defaultSchema != null && defaultSchema.length() == 0)
{ {
@@ -1021,7 +1033,7 @@ public class SchemaBootstrap extends AbstractLifecycleBean
setJobExecutorActivate(false). setJobExecutorActivate(false).
buildProcessEngine(); buildProcessEngine();
String schemaName = dbSchemaName != null ? dbSchemaName : DatabaseMetaDataHelper.getSchema(connection); String schemaName = dbSchemaName != null ? dbSchemaName : databaseMetaDataHelper.getSchema(connection);
// create or upgrade the DB schema // create or upgrade the DB schema
engine.getManagementService().databaseSchemaUpgrade(connection, null, schemaName); engine.getManagementService().databaseSchemaUpgrade(connection, null, schemaName);
} }
@@ -1774,6 +1786,17 @@ public class SchemaBootstrap extends AbstractLifecycleBean
{ {
logger.warn("Error closing DB connection: " + e.getMessage()); logger.warn("Error closing DB connection: " + e.getMessage());
} }
try
{
if (session != null)
{
session.close();
}
}
catch (Throwable e)
{
logger.warn("Error closing Hibernate session: " + e.getMessage());
}
// Remove the connection reference from the threadlocal boostrap // Remove the connection reference from the threadlocal boostrap
SchemaBootstrapConnectionProvider.setBootstrapConnection(null); SchemaBootstrapConnectionProvider.setBootstrapConnection(null);
@@ -1866,7 +1889,7 @@ public class SchemaBootstrap extends AbstractLifecycleBean
XMLToSchema xmlToSchema = new XMLToSchema(is); XMLToSchema xmlToSchema = new XMLToSchema(is);
xmlToSchema.parse(); xmlToSchema.parse();
Schema reference = xmlToSchema.getSchema(); Schema reference = xmlToSchema.getSchema();
ExportDb exporter = new ExportDb(dataSource, dialect, descriptorService); ExportDb exporter = new ExportDb(dataSource, dialect, descriptorService, databaseMetaDataHelper);
exporter.setDbSchemaName(dbSchemaName); exporter.setDbSchemaName(dbSchemaName);
// Ensure that the database objects we're validating are filtered // Ensure that the database objects we're validating are filtered
// by the same prefix as the reference file. // by the same prefix as the reference file.

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2012 Alfresco Software Limited. * Copyright (C) 2005-2014 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -24,6 +24,8 @@ import java.sql.ResultSet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
/** /**
* Helper class to collect all of our DatabaseMetaData interpretations in one place. * Helper class to collect all of our DatabaseMetaData interpretations in one place.
@@ -35,12 +37,19 @@ public class DatabaseMetaDataHelper {
private static Log logger = LogFactory.getLog(DatabaseMetaDataHelper.class); private static Log logger = LogFactory.getLog(DatabaseMetaDataHelper.class);
private Configuration cfg;
public void setLocalSessionFactory(LocalSessionFactoryBean localSessionFactory)
{
this.cfg = localSessionFactory.getConfiguration();
}
/** /**
* Trys to determine the schema name from the DatabaseMetaData obtained from the Connection. * Trys to determine the schema name from the DatabaseMetaData obtained from the Connection.
* @param connection A database connection * @param connection A database connection
* @return * @return
*/ */
public static String getSchema(Connection connection) private String getSchemaFromConnection(Connection connection)
{ {
if (connection == null) { if (connection == null) {
@@ -89,4 +98,33 @@ public class DatabaseMetaDataHelper {
} }
return null; return null;
} }
public String getSchema(Connection connection)
{
String schema = null;
if (this.cfg != null)
{
String tmpSchema = this.cfg.getProperty("hibernate.default_schema");
if (tmpSchema != null && tmpSchema.trim().length() > 0)
{
schema = tmpSchema;
}
}
// if hibernate.default_schema was specified as a system property, then override previous value
String tmpSchema = System.getProperty("hibernate.default_schema");
if (tmpSchema != null && tmpSchema.length() > 0)
{
schema = tmpSchema;
}
if (schema == null)
{
schema = getSchemaFromConnection(connection);
}
return schema;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2013 Alfresco Software Limited. * Copyright (C) 2005-2011 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -30,6 +30,7 @@ import javax.sql.DataSource;
import org.alfresco.service.descriptor.Descriptor; import org.alfresco.service.descriptor.Descriptor;
import org.alfresco.service.descriptor.DescriptorService; import org.alfresco.service.descriptor.DescriptorService;
import org.alfresco.util.DatabaseMetaDataHelper;
import org.alfresco.util.PropertyCheck; import org.alfresco.util.PropertyCheck;
import org.alfresco.util.schemacomp.model.Column; import org.alfresco.util.schemacomp.model.Column;
import org.alfresco.util.schemacomp.model.ForeignKey; import org.alfresco.util.schemacomp.model.ForeignKey;
@@ -55,6 +56,9 @@ public class ExportDb
/** Reverse map from database types to JDBC types (loaded from a Hibernate dialect). */ /** Reverse map from database types to JDBC types (loaded from a Hibernate dialect). */
private final Map<String, Integer> reverseTypeMap = new TreeMap<String, Integer>(); private final Map<String, Integer> reverseTypeMap = new TreeMap<String, Integer>();
/** The database metadata helper */
private DatabaseMetaDataHelper databaseMetaDataHelper;
/** The JDBC DataSource. */ /** The JDBC DataSource. */
private DataSource dataSource; private DataSource dataSource;
@@ -80,7 +84,8 @@ public class ExportDb
{ {
this((DataSource) context.getBean("dataSource"), this((DataSource) context.getBean("dataSource"),
(Dialect) context.getBean("dialect"), (Dialect) context.getBean("dialect"),
(DescriptorService) context.getBean("descriptorComponent")); (DescriptorService) context.getBean("descriptorComponent"),
(DatabaseMetaDataHelper) context.getBean("databaseMetaDataHelper"));
} }
@@ -90,11 +95,12 @@ public class ExportDb
* @param connection the database connection to use for metadata queries * @param connection the database connection to use for metadata queries
* @param dialect the Hibernate dialect * @param dialect the Hibernate dialect
*/ */
public ExportDb(final DataSource dataSource, final Dialect dialect, DescriptorService descriptorService) public ExportDb(final DataSource dataSource, final Dialect dialect, DescriptorService descriptorService, DatabaseMetaDataHelper databaseMetaDataHelper)
{ {
this.dataSource = dataSource; this.dataSource = dataSource;
this.dialect = dialect; this.dialect = dialect;
this.descriptorService = descriptorService; this.descriptorService = descriptorService;
this.databaseMetaDataHelper = databaseMetaDataHelper;
init(); init();
} }
@@ -202,7 +208,7 @@ public class ExportDb
{ {
final DatabaseMetaData dbmd = con.getMetaData(); final DatabaseMetaData dbmd = con.getMetaData();
String schemaName = getSchemaName(dbmd); String schemaName = databaseMetaDataHelper.getSchema(con);
schema = new Schema(schemaName, namePrefix, schemaVersion, true); schema = new Schema(schemaName, namePrefix, schemaVersion, true);
String[] prefixFilters = namePrefixFilters(dbmd); String[] prefixFilters = namePrefixFilters(dbmd);
@@ -383,36 +389,6 @@ public class ExportDb
tables.close(); tables.close();
} }
/**
* Assume that if there are schemas, we want the one named after the connection user
* or the one called "dbo" (MS SQL hack)
*
* @param dbmd
* @return The schema name, or null otherwise.
* @throws SQLException
*/
private String getSchemaName(final DatabaseMetaData dbmd) throws SQLException
{
if (this.dbSchemaName != null)
{
return this.dbSchemaName;
}
String schemaName = null;
final ResultSet schemas = dbmd.getSchemas();
while (schemas.next())
{
final String thisSchema = schemas.getString("TABLE_SCHEM");
if (thisSchema.equalsIgnoreCase(dbmd.getUserName()) || thisSchema.equalsIgnoreCase("dbo"))
{
schemaName = thisSchema;
break;
}
}
schemas.close();
return schemaName;
}
/** /**
* Convert a boolean string as used in the database, to a boolean value. * Convert a boolean string as used in the database, to a boolean value.