Skip to content
Snippets Groups Projects
Commit ff84f0b8 authored by Vincent Massol's avatar Vincent Massol
Browse files

XWIKI-1740: Add full xwiki virtual mode support for HSQLDB

+ Removed deprecated usage of session.connection()
+ Added configuration options (commented out) for using BoneCP with Hibernate (ATM using DBCP fails with HSQLDB for some unknown reasons but it seems to work fine with BoneCP - Need to be tested more though)
parent 25d4d3b8
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,7 @@
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
......@@ -42,6 +43,7 @@
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.jdbc.BorrowedConnectionProxy;
import org.hibernate.jdbc.ConnectionManager;
import org.hibernate.jdbc.Work;
import org.hibernate.mapping.Table;
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
import org.slf4j.Logger;
......@@ -380,6 +382,12 @@ protected String getSchemaFromWikiName(String wikiName, DatabaseProduct database
} else {
// virtual
schema = wikiName.replace('-', '_');
// For HSQLDB we only support uppercase schema names. This is because Hibernate doesn't properly generate
// quotes around schema names when it qualifies the table name when it generates the update script.
if (databaseProduct == DatabaseProduct.HSQLDB) {
schema = StringUtils.upperCase(schema);
}
}
// Apply prefix
......@@ -454,7 +462,7 @@ public String[] getSchemaUpdateScript(Configuration config, XWikiContext context
String contextSchema = getSchemaFromWikiName(context);
DatabaseProduct databaseProduct = getDatabaseProductName(context);
DatabaseProduct databaseProduct = getDatabaseProductName();
if (databaseProduct == DatabaseProduct.ORACLE || databaseProduct == DatabaseProduct.HSQLDB
|| databaseProduct == DatabaseProduct.DERBY || databaseProduct == DatabaseProduct.DB2) {
dschema = config.getProperty(Environment.DEFAULT_SCHEMA);
......@@ -625,32 +633,11 @@ public void setDatabase(Session session, XWikiContext context) throws XWikiExcep
DatabaseProduct databaseProduct = getDatabaseProductName(context);
if (DatabaseProduct.ORACLE == databaseProduct) {
Statement stmt = null;
try {
stmt = session.connection().createStatement();
stmt.execute("alter session set current_schema = " + escapedSchemaName);
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
}
}
executeSQL("alter session set current_schema = " + escapedSchemaName, session);
} else if (DatabaseProduct.DERBY == databaseProduct || DatabaseProduct.HSQLDB == databaseProduct
|| DatabaseProduct.DB2 == databaseProduct) {
Statement stmt = null;
try {
stmt = session.connection().createStatement();
stmt.execute("SET SCHEMA " + escapedSchemaName);
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
}
}
|| DatabaseProduct.DB2 == databaseProduct)
{
executeSQL("SET SCHEMA " + schemaName, session);
} else {
String catalog = session.connection().getCatalog();
catalog = (catalog == null) ? null : catalog.replace('_', '-');
......@@ -672,6 +659,33 @@ public void setDatabase(Session session, XWikiContext context) throws XWikiExcep
}
}
/**
* Execute an SQL statement using Hibernate.
*
* @param sql the SQL statement to execute
* @param session the Hibernate Session in which to execute the statement
*/
private void executeSQL(final String sql, Session session)
{
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException
{
Statement stmt = null;
try {
stmt = connection.createStatement();
stmt.execute(sql);
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
}
}
}
});
}
/**
* Escape schema name depending of the database engine.
*
......
......@@ -32,7 +32,7 @@
http://platform.xwiki.org/xwiki/bin/view/AdminGuide/Installation for configuring your
database. You'll need to do 2 things:
1) Copy your database driver JAR in WEB-INF/lib or in some shared lib directory
2) Uncomment the properties below for your specific DB (and comment the default
2) Uncomment the properties below for your specific DB (and comment the default
database configuration if it doesn't match your DB)
-->
......@@ -50,6 +50,19 @@
<property name="dbcp.ps.maxActive">20</property>
<property name="dbcp.ps.maxWait">120000</property>
<property name="dbcp.ps.maxIdle">20</property>
<!-- Uncomment to use BoneCP Connection Pooling -->
<!--
<property name="bonecp.idleMaxAgeInMinutes">240</property>
<property name="bonecp.idleConnectionTestPeriodInMinutes">60</property>
<property name="bonecp.partitionCount">3</property>
<property name="bonecp.acquireIncrement">10</property>
<property name="bonecp.maxConnectionsPerPartition">60</property>
<property name="bonecp.minConnectionsPerPartition">20</property>
<property name="bonecp.statementsCacheSize">50</property>
<property name="bonecp.releaseHelperThreads">3</property>
-->
<!-- Without it, some queries fail in MS SQL. XWiki doesn't need scrollable result sets, anyway. -->
<property name="jdbc.use_scrollable_resultset">false</property>
......@@ -58,13 +71,16 @@
Note that the database tables will be created automatically if they don't already exist.
-->
## Only use a default configuration if the $xwikiDbConnectionUrl field equal to "none". I would
## have preferred to use an empty value but I couldn't make that work with Maven.
## have preferred to use an empty value but I couldn't make that work with Maven.
#if ($xwikiDbConnectionUrl != "none")
<property name="connection.url">$xwikiDbConnectionUrl</property>
<property name="connection.username">$!xwikiDbConnectionUsername</property>
<property name="connection.password">$!xwikiDbConnectionPassword</property>
<property name="connection.driver_class">$xwikiDbConnectionDriverClass</property>
<property name="dialect">$xwikiDbDialect</property>
<!-- Uncomment to use BoneCP Connection Pooling. You'll also need to comment out the DBCP property of the same name
below since there can be only one such property. -->
<!--property name="connection.provider_class">com.xpn.xwiki.store.DBCPConnectionProvider</property-->
<property name="connection.provider_class">com.xpn.xwiki.store.DBCPConnectionProvider</property>
<property name="connection.pool_size">2</property>
<property name="statement_cache.size">2</property>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment