use of org.alfresco.repo.domain.dialect.PostgreSQLDialect in project alfresco-repository by Alfresco.
the class SchemaBootstrap method executeScriptFile.
/**
* @param connection the DB connection to use
* @param scriptFile the file containing the statements
* @param scriptUrl the URL of the script to report. If this is null, the script
* is assumed to have been auto-generated.
*/
private void executeScriptFile(Connection connection, File scriptFile, String scriptUrl) throws Exception {
final Dialect dialect = this.dialect;
if (executedStatementsThreadLocal.get() == null) {
// Validate the schema, pre-upgrade
validateSchema("Alfresco-{0}-Validation-Pre-Upgrade-{1}-", null);
dumpSchema("pre-upgrade");
// There is no lock at this stage. This process can fall out if the lock can't be applied.
setBootstrapStarted(connection);
executedStatementsThreadLocal.set(new StringBuilder(8094));
}
if (scriptUrl == null) {
LogUtil.info(logger, MSG_EXECUTING_GENERATED_SCRIPT, scriptFile);
} else {
LogUtil.info(logger, MSG_EXECUTING_COPIED_SCRIPT, scriptFile, scriptUrl);
}
InputStream scriptInputStream = new FileInputStream(scriptFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(scriptInputStream, "UTF-8"));
try {
int line = 0;
// loop through all statements
StringBuilder sb = new StringBuilder(1024);
String fetchVarName = null;
String fetchColumnName = null;
Object defaultFetchValue = null;
boolean doBatch = false;
int batchUpperLimit = 0;
int batchSize = 1;
Map<String, Object> varAssignments = new HashMap<String, Object>(13);
String delimiter = ";";
// Special variable assignments:
if (dialect instanceof PostgreSQLDialect) {
// Needs 1/0 for true/false
varAssignments.put("true", "true");
varAssignments.put("false", "false");
varAssignments.put("TRUE", "TRUE");
varAssignments.put("FALSE", "FALSE");
} else {
// Needs true/false as strings
varAssignments.put("true", "1");
varAssignments.put("false", "0");
varAssignments.put("TRUE", "1");
varAssignments.put("FALSE", "0");
}
long now = System.currentTimeMillis();
varAssignments.put("now", new Long(now).toString());
varAssignments.put("NOW", new Long(now).toString());
while (true) {
String sqlOriginal = reader.readLine();
line++;
if (sqlOriginal == null) {
// nothing left in the file
break;
}
// trim it
String sql = sqlOriginal.trim();
// Check of includes
if (sql.startsWith("--INCLUDE:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_INCLUDE_BEFORE_SQL, (line - 1), scriptUrl);
}
String includedScriptUrl = sql.substring(10, sql.length());
// Execute the script in line
executeScriptUrl(connection, includedScriptUrl);
} else // Check for variable assignment
if (sql.startsWith("--ASSIGN:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_VAR_ASSIGNMENT_BEFORE_SQL, (line - 1), scriptUrl);
}
String assignStr = sql.substring(9, sql.length());
String[] fetchMapping = assignStr.split("!");
String[] assigns = fetchMapping[0].split("=");
if (assigns.length != 2 || assigns[0].length() == 0 || assigns[1].length() == 0) {
throw AlfrescoRuntimeException.create(ERR_STATEMENT_VAR_ASSIGNMENT_FORMAT, (line - 1), scriptUrl);
}
fetchVarName = assigns[0];
fetchColumnName = assigns[1];
if (fetchMapping.length > 1 && fetchMapping[1].length() > 0) {
defaultFetchValue = fetchMapping[1];
}
continue;
} else // Handle looping control
if (sql.startsWith("--FOREACH")) {
// --FOREACH table.column batch.size.property
String[] args = sql.split("[ \\t]+");
int sepIndex;
if (args.length == 3 && (sepIndex = args[1].indexOf('.')) != -1) {
doBatch = true;
// Select the upper bound of the table column
String stmt = "SELECT MAX(" + args[1].substring(sepIndex + 1) + ") AS upper_limit FROM " + args[1].substring(0, sepIndex);
Object fetchedVal = executeStatement(connection, stmt, "upper_limit", false, line, scriptFile);
if (fetchedVal instanceof Number) {
batchUpperLimit = ((Number) fetchedVal).intValue();
// Read the batch size from the named property
String batchSizeString = globalProperties.getProperty(args[2]);
// Fall back to the default property
if (batchSizeString == null) {
batchSizeString = globalProperties.getProperty(PROPERTY_DEFAULT_BATCH_SIZE);
}
batchSize = batchSizeString == null ? 10000 : Integer.parseInt(batchSizeString);
}
}
continue;
} else // Allow transaction delineation
if (sql.startsWith("--BEGIN TXN")) {
connection.setAutoCommit(false);
continue;
} else if (sql.startsWith("--END TXN")) {
connection.commit();
connection.setAutoCommit(true);
continue;
} else if (sql.startsWith("--SET-DELIMITER:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_DELIMITER_SET_BEFORE_SQL, (line - 1), scriptUrl);
}
// We're good...so set the new delimiter
String newDelim = sql.substring(16).trim();
if (newDelim.length() == 0) {
throw AlfrescoRuntimeException.create(ERR_DELIMITER_INVALID, (line - 1), scriptUrl);
}
delimiter = newDelim;
}
// Check for comments
if (sql.length() == 0 || sql.startsWith("--") || sql.startsWith("//") || sql.startsWith("/*")) {
if (sb.length() > 0) {
// we have an unterminated statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_TERMINATOR, delimiter, (line - 1), scriptUrl);
}
// there has not been anything to execute - it's just a comment line
continue;
}
// have we reached the end of a statement?
boolean execute = false;
boolean optional = false;
if (sql.endsWith(delimiter)) {
sql = sql.substring(0, sql.length() - 1);
execute = true;
optional = false;
} else if (sql.endsWith("(optional)") || sql.endsWith("(OPTIONAL)")) {
// Get the end of statement
int endIndex = sql.lastIndexOf(delimiter);
if (endIndex > -1) {
sql = sql.substring(0, endIndex);
execute = true;
optional = true;
} else {
// Ends with "(optional)" but there is no semi-colon.
// Just take it at face value and probably fail.
}
}
// Add newline
if (sb.length() > 0) {
sb.append("\n");
}
// Add leading whitespace for formatting
int whitespaceCount = sqlOriginal.indexOf(sql);
for (int i = 0; i < whitespaceCount; i++) {
sb.append(" ");
}
// append to the statement being built up
sb.append(sql);
// execute, if required
if (execute) {
// Now substitute and execute the statement the appropriate number of times
String unsubstituted = sb.toString();
for (int lowerBound = 0; lowerBound <= batchUpperLimit; lowerBound += batchSize) {
sql = unsubstituted;
// Substitute in the next pair of range parameters
if (doBatch) {
varAssignments.put("LOWERBOUND", String.valueOf(lowerBound));
varAssignments.put("UPPERBOUND", String.valueOf(lowerBound + batchSize - 1));
}
// Perform variable replacement using the ${var} format
for (Map.Entry<String, Object> entry : varAssignments.entrySet()) {
String var = entry.getKey();
Object val = entry.getValue();
sql = sql.replaceAll("\\$\\{" + var + "\\}", val.toString());
}
// Handle the 0/1 values that PostgreSQL doesn't translate to TRUE
if (this.dialect != null && this.dialect instanceof PostgreSQLDialect) {
sql = sql.replaceAll("\\$\\{TRUE\\}", "TRUE");
} else {
sql = sql.replaceAll("\\$\\{TRUE\\}", "1");
}
if (this.dialect != null && this.dialect instanceof MySQLInnoDBDialect) {
// note: enable bootstrap on MySQL 5.5 (eg. for auto-generated SQL)
sql = sql.replaceAll("(?i)TYPE=InnoDB", "ENGINE=InnoDB");
}
if (this.dialect != null && this.dialect instanceof MySQLClusterNDBDialect) {
// note: enable bootstrap on MySQL Cluster NDB
/*
* WARNING: Experimental/unsupported - see MySQLClusterNDBDialect !
*/
// belts-and-braces
sql = sql.replaceAll("(?i)TYPE=InnoDB", "ENGINE=NDB");
sql = sql.replaceAll("(?i)ENGINE=InnoDB", "ENGINE=NDB");
sql = sql.replaceAll("(?i) BIT ", " BOOLEAN ");
sql = sql.replaceAll("(?i) BIT,", " BOOLEAN,");
sql = sql.replaceAll("(?i) string_value text", " string_value VARCHAR(" + DEFAULT_MAX_STRING_LENGTH_NDB + ")");
sql = sql.replaceAll("(?i) VARCHAR(4000)", "TEXT(4000)");
}
Object fetchedVal = executeStatement(connection, sql, fetchColumnName, optional, line, scriptFile);
if (fetchVarName != null && fetchColumnName != null) {
if (fetchedVal != null) {
varAssignments.put(fetchVarName, fetchedVal);
} else {
varAssignments.put(fetchVarName, defaultFetchValue);
}
}
}
sb.setLength(0);
fetchVarName = null;
fetchColumnName = null;
defaultFetchValue = null;
doBatch = false;
batchUpperLimit = 0;
batchSize = 1;
}
}
} finally {
try {
reader.close();
} catch (Throwable e) {
}
try {
scriptInputStream.close();
} catch (Throwable e) {
}
}
}
use of org.alfresco.repo.domain.dialect.PostgreSQLDialect in project alfresco-repository by Alfresco.
the class ScriptExecutorImpl method executeScriptFile.
/**
* @param connection the DB connection to use
* @param scriptFile the file containing the statements
* @param scriptUrl the URL of the script to report. If this is null, the script
* is assumed to have been auto-generated.
*/
private void executeScriptFile(Connection connection, File scriptFile, String scriptUrl) throws Exception {
final Dialect dialect = this.dialect;
StringBuilder executedStatements = executedStatementsThreadLocal.get();
if (executedStatements == null) {
executedStatements = new StringBuilder(8094);
executedStatementsThreadLocal.set(executedStatements);
}
if (scriptUrl == null) {
LogUtil.info(logger, MSG_EXECUTING_GENERATED_SCRIPT, scriptFile);
} else {
LogUtil.info(logger, MSG_EXECUTING_COPIED_SCRIPT, scriptFile, scriptUrl);
}
InputStream scriptInputStream = new FileInputStream(scriptFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(scriptInputStream, "UTF-8"));
try {
int line = 0;
// loop through all statements
StringBuilder sb = new StringBuilder(1024);
String fetchVarName = null;
String fetchColumnName = null;
Object defaultFetchValue = null;
String batchTableName = null;
boolean doBatch = false;
int batchUpperLimit = 0;
int batchSize = 1;
Map<String, Object> varAssignments = new HashMap<String, Object>(13);
String delimiter = ";";
// Special variable assignments:
if (dialect instanceof PostgreSQLDialect) {
// Needs 1/0 for true/false
varAssignments.put("true", "true");
varAssignments.put("false", "false");
varAssignments.put("TRUE", "TRUE");
varAssignments.put("FALSE", "FALSE");
} else {
// Needs true/false as strings
varAssignments.put("true", "1");
varAssignments.put("false", "0");
varAssignments.put("TRUE", "1");
varAssignments.put("FALSE", "0");
}
long now = System.currentTimeMillis();
varAssignments.put("now", new Long(now).toString());
varAssignments.put("NOW", new Long(now).toString());
while (true) {
String sqlOriginal = reader.readLine();
line++;
if (sqlOriginal == null) {
// nothing left in the file
break;
}
// trim it
String sql = sqlOriginal.trim();
// Check of includes
if (sql.startsWith("--INCLUDE:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_INCLUDE_BEFORE_SQL, (line - 1), scriptUrl);
}
String includedScriptUrl = sql.substring(10, sql.length());
// Execute the script in line
executeScriptUrl(connection, includedScriptUrl);
} else // Check for variable assignment
if (sql.startsWith("--ASSIGN:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_VAR_ASSIGNMENT_BEFORE_SQL, (line - 1), scriptUrl);
}
String assignStr = sql.substring(9, sql.length());
String[] fetchMapping = assignStr.split("!");
String[] assigns = fetchMapping[0].split("=");
if (assigns.length != 2 || assigns[0].length() == 0 || assigns[1].length() == 0) {
throw AlfrescoRuntimeException.create(ERR_STATEMENT_VAR_ASSIGNMENT_FORMAT, (line - 1), scriptUrl);
}
fetchVarName = assigns[0];
fetchColumnName = assigns[1];
if (fetchMapping.length > 1 && fetchMapping[1].length() > 0) {
defaultFetchValue = fetchMapping[1];
}
continue;
} else // Handle looping control
if (sql.startsWith("--FOREACH")) {
// --FOREACH table.column batch.size.property
String[] args = sql.split("[ \\t]+");
int sepIndex;
if (args.length == 3 && (sepIndex = args[1].indexOf('.')) != -1) {
doBatch = true;
// Select the upper bound of the table column
batchTableName = args[1].substring(0, sepIndex);
String stmt = "SELECT MAX(" + args[1].substring(sepIndex + 1) + ") AS upper_limit FROM " + batchTableName;
Object fetchedVal = executeStatement(connection, stmt, "upper_limit", false, line, scriptFile);
if (fetchedVal instanceof Number) {
batchUpperLimit = ((Number) fetchedVal).intValue();
// Read the batch size from the named property
String batchSizeString = globalProperties.getProperty(args[2]);
// Fall back to the default property
if (batchSizeString == null) {
batchSizeString = globalProperties.getProperty(PROPERTY_DEFAULT_BATCH_SIZE);
}
batchSize = batchSizeString == null ? 10000 : Integer.parseInt(batchSizeString);
}
}
continue;
} else if (sql.startsWith("--DELETE_NOT_EXISTS")) {
DeleteNotExistsExecutor deleteNotExists = new DeleteNotExistsExecutor(connection, sql, line, scriptFile, globalProperties);
deleteNotExists.execute();
// Reset
sb.setLength(0);
fetchVarName = null;
fetchColumnName = null;
defaultFetchValue = null;
batchTableName = null;
doBatch = false;
batchUpperLimit = 0;
batchSize = 1;
continue;
} else // Allow transaction delineation
if (sql.startsWith("--BEGIN TXN")) {
connection.setAutoCommit(false);
continue;
} else if (sql.startsWith("--END TXN")) {
connection.commit();
connection.setAutoCommit(true);
continue;
} else if (sql.startsWith("--SET-DELIMITER:")) {
if (sb.length() > 0) {
// This can only be set before a new SQL statement
throw AlfrescoRuntimeException.create(ERR_DELIMITER_SET_BEFORE_SQL, (line - 1), scriptUrl);
}
// We're good...so set the new delimiter
String newDelim = sql.substring(16).trim();
if (newDelim.length() == 0) {
throw AlfrescoRuntimeException.create(ERR_DELIMITER_INVALID, (line - 1), scriptUrl);
}
delimiter = newDelim;
}
// Check for comments
if (sql.length() == 0 || sql.startsWith("--") || sql.startsWith("//") || sql.startsWith("/*")) {
if (sb.length() > 0) {
// we have an unterminated statement
throw AlfrescoRuntimeException.create(ERR_STATEMENT_TERMINATOR, delimiter, (line - 1), scriptUrl);
}
// there has not been anything to execute - it's just a comment line
continue;
}
// have we reached the end of a statement?
boolean execute = false;
boolean optional = false;
if (sql.endsWith(delimiter)) {
sql = sql.substring(0, sql.length() - 1);
execute = true;
optional = false;
} else if (sql.endsWith("(optional)") || sql.endsWith("(OPTIONAL)")) {
// Get the end of statement
int endIndex = sql.lastIndexOf(delimiter);
if (endIndex > -1) {
sql = sql.substring(0, endIndex);
execute = true;
optional = true;
} else {
// Ends with "(optional)" but there is no semi-colon.
// Just take it at face value and probably fail.
}
}
// Add newline
if (sb.length() > 0) {
sb.append("\n");
}
// Add leading whitespace for formatting
int whitespaceCount = sqlOriginal.indexOf(sql);
for (int i = 0; i < whitespaceCount; i++) {
sb.append(" ");
}
// append to the statement being built up
sb.append(sql);
// execute, if required
if (execute) {
// Now substitute and execute the statement the appropriate number of times
String unsubstituted = sb.toString();
for (int lowerBound = 0; lowerBound <= batchUpperLimit; lowerBound += batchSize) {
sql = unsubstituted;
// Substitute in the next pair of range parameters
if (doBatch) {
logger.info("Processing from " + lowerBound + " to " + (lowerBound + batchSize) + " rows of " + batchUpperLimit + " rows from table " + batchTableName + ".");
varAssignments.put("LOWERBOUND", String.valueOf(lowerBound));
varAssignments.put("UPPERBOUND", String.valueOf(lowerBound + batchSize - 1));
}
// Perform variable replacement using the ${var} format
for (Map.Entry<String, Object> entry : varAssignments.entrySet()) {
String var = entry.getKey();
Object val = entry.getValue();
sql = sql.replaceAll("\\$\\{" + var + "\\}", val.toString());
}
// Handle the 0/1 values that PostgreSQL doesn't translate to TRUE
if (this.dialect != null && this.dialect instanceof PostgreSQLDialect) {
sql = sql.replaceAll("\\$\\{TRUE\\}", "TRUE");
} else {
sql = sql.replaceAll("\\$\\{TRUE\\}", "1");
}
if (this.dialect != null && this.dialect instanceof MySQLInnoDBDialect) {
// note: enable bootstrap on MySQL 5.5 (eg. for auto-generated SQL)
sql = sql.replaceAll("(?i)TYPE=InnoDB", "ENGINE=InnoDB");
}
if (this.dialect != null && this.dialect instanceof MySQLClusterNDBDialect) {
// note: enable bootstrap on MySQL Cluster NDB
/*
* WARNING: Experimental/unsupported - see MySQLClusterNDBDialect !
*/
// belts-and-braces
sql = sql.replaceAll("(?i)TYPE=InnoDB", "ENGINE=NDB");
sql = sql.replaceAll("(?i)ENGINE=InnoDB", "ENGINE=NDB");
sql = sql.replaceAll("(?i) BIT ", " BOOLEAN ");
sql = sql.replaceAll("(?i) BIT,", " BOOLEAN,");
sql = sql.replaceAll("(?i) string_value text", " string_value VARCHAR(" + DEFAULT_MAX_STRING_LENGTH_NDB + ")");
sql = sql.replaceAll("(?i) VARCHAR(4000)", "TEXT(4000)");
}
Object fetchedVal = executeStatement(connection, sql, fetchColumnName, optional, line, scriptFile);
if (fetchVarName != null && fetchColumnName != null) {
if (fetchedVal == null) {
fetchedVal = defaultFetchValue;
}
// We must have some value
if (fetchedVal == null) {
// The variable is null (not even empty)
throw AlfrescoRuntimeException.create(ERR_STATEMENT_VAR_ASSIGNMENT_NULL, fetchVarName, fetchVarName, (line - 1), scriptUrl);
}
varAssignments.put(fetchVarName, fetchedVal);
}
}
sb.setLength(0);
fetchVarName = null;
fetchColumnName = null;
defaultFetchValue = null;
batchTableName = null;
doBatch = false;
batchUpperLimit = 0;
batchSize = 1;
}
}
} finally {
try {
reader.close();
} catch (Throwable e) {
}
try {
scriptInputStream.close();
} catch (Throwable e) {
}
}
}
use of org.alfresco.repo.domain.dialect.PostgreSQLDialect in project alfresco-repository by Alfresco.
the class SchemaBootstrap method checkDialect.
/**
* Performs dialect-specific checking. This includes checking for InnoDB, dumping the dialect being used
* as well as setting any runtime, dialect-specific properties.
*/
private void checkDialect(Dialect dialect) {
Class<?> dialectClazz = dialect.getClass();
LogUtil.info(logger, MSG_DIALECT_USED, dialectClazz.getName());
// if (dialectClazz.equals(MySQLDialect.class) || dialectClazz.equals(MySQL5Dialect.class))
// {
// LogUtil.error(logger, ERR_DIALECT_SHOULD_USE, dialectClazz.getName(), MySQLInnoDBDialect.class.getName());
// throw AlfrescoRuntimeException.create(WARN_DIALECT_UNSUPPORTED, dialectClazz.getName());
// }
// else if (dialectClazz.equals(HSQLDialect.class))
// {
// LogUtil.info(logger, WARN_DIALECT_HSQL);
// }
// else if (dialectClazz.equals(DerbyDialect.class))
// {
// LogUtil.info(logger, WARN_DIALECT_DERBY);
// }
// else if (dialectClazz.equals(Oracle9iDialect.class) || dialectClazz.equals(Oracle10gDialect.class))
// {
// LogUtil.error(logger, ERR_DIALECT_SHOULD_USE, dialectClazz.getName(), Oracle9Dialect.class.getName());
// throw AlfrescoRuntimeException.create(WARN_DIALECT_UNSUPPORTED, dialectClazz.getName());
// }
// else if (dialectClazz.equals(OracleDialect.class) || dialectClazz.equals(Oracle9Dialect.class))
// {
// LogUtil.error(logger, ERR_DIALECT_SHOULD_USE, dialectClazz.getName(), Oracle9Dialect.class.getName());
// throw AlfrescoRuntimeException.create(WARN_DIALECT_UNSUPPORTED, dialectClazz.getName());
// }
int maxStringLength = SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH;
int serializableType = SerializableTypeHandler.getSerializableType();
// Adjust the maximum allowable String length according to the dialect
if (dialect instanceof SQLServerDialect) {
// string_value nvarchar(1024) null,
// serializable_value image null,
maxStringLength = SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH;
} else if (dialect instanceof MySQLClusterNDBDialect) {
// string_value varchar(400),
// serializable_value blob,
maxStringLength = SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH_NDB;
} else if (dialect instanceof MySQLInnoDBDialect) {
// string_value text,
// serializable_value blob,
maxStringLength = Integer.MAX_VALUE;
} else if (dialect instanceof Oracle9Dialect) {
// string_value varchar2(1024 char),
// serializable_value blob,
maxStringLength = SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH;
} else if (dialect instanceof PostgreSQLDialect) {
// string_value varchar(1024),
// serializable_value bytea,
maxStringLength = SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH;
}
SchemaBootstrap.setMaxStringLength(maxStringLength, dialect);
SerializableTypeHandler.setSerializableType(serializableType);
// Now override the maximum string length if it was set directly
if (maximumStringLength > 0) {
SchemaBootstrap.setMaxStringLength(maximumStringLength, dialect);
}
}
use of org.alfresco.repo.domain.dialect.PostgreSQLDialect in project alfresco-repository by Alfresco.
the class SearcherComponentTest method setUp.
@Before
public void setUp() throws Exception {
dialect = (Dialect) applicationContext.getBean("dialect");
if (dialect instanceof PostgreSQLDialect) {
// Note: PostgreSQL does not support \u0000 char embedded in a string
// http://archives.postgresql.org/pgsql-jdbc/2007-02/msg00115.php
COMPLEX_LOCAL_NAME = COMPLEX_LOCAL_NAME_NO_U0000;
}
serviceRegistry = (ServiceRegistry) applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
dictionaryService = BaseNodeServiceTest.loadModel(applicationContext);
nodeService = serviceRegistry.getNodeService();
this.authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
this.authenticationComponent.setSystemUserAsCurrentUser();
// get the indexer and searcher factory
IndexerAndSearcher indexerAndSearcher = (IndexerAndSearcher) applicationContext.getBean("indexerAndSearcherFactory");
searcher = new SearcherComponent();
searcher.setIndexerAndSearcherFactory(indexerAndSearcher);
// create a test workspace
StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + "_" + System.currentTimeMillis());
rootNodeRef = nodeService.getRootNode(storeRef);
// begin a transaction
txn = transactionService.getUserTransaction();
txn.begin();
}
use of org.alfresco.repo.domain.dialect.PostgreSQLDialect in project alfresco-repository by Alfresco.
the class TransactionServiceImplTest method testReadOnlyTxn.
@Test
public void testReadOnlyTxn() throws Exception {
// start a read-only transaction
transactionService.setAllowWrite(false, vetoName);
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
// do some writing
try {
nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + "_" + System.currentTimeMillis());
txn.commit();
fail("Read-only transaction wasn't detected");
} catch (ReadOnlyServerException e) {
// This is now thrown at the lower layers, but it *is* possible for one of the later
// exceptions to get through: Fixed ALF-3884: Share does not report access denied exceptions correctly
@SuppressWarnings("unused") int i = 0;
} catch (InvalidDataAccessApiUsageException e) {
// expected this ...
@SuppressWarnings("unused") int i = 0;
} catch (TransientDataAccessResourceException e) {
// or this - for MySQL (java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.)
@SuppressWarnings("unused") int i = 0;
} catch (IllegalStateException e) {
// or this - for MS SQLServer, Oracle (via AbstractNodeDAOImpl.getCurrentTransaction)
@SuppressWarnings("unused") int i = 0;
} catch (UncategorizedSQLException e) {
// or this - for PostgreSQL (org.postgresql.util.PSQLException: ERROR: transaction is read-only)
if (dialect instanceof PostgreSQLDialect) {
// ALF-4226
@SuppressWarnings("unused") int i = 0;
} else {
throw e;
}
} finally {
transactionService.setAllowWrite(true, vetoName);
try {
txn.rollback();
} catch (Throwable e) {
}
}
}
Aggregations