Skip to content

Instantly share code, notes, and snippets.

@blalor
Created February 15, 2011 16:08
Show Gist options
  • Save blalor/827708 to your computer and use it in GitHub Desktop.
Save blalor/827708 to your computer and use it in GitHub Desktop.
Spring factory for Hsqldb URLs

Spring glue code for HSQLDB

HSQLDB doesn't play that well with Spring when using an in-memory data source. This code provides a HSQLDB URL factory so that a URL can be built from a org.springframework.core.io.Resource, and also shows how the DAO must explicitly invoke the shutdown query.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-lazy-init="true">
<bean id="hsqldbUrl" class="….HsqldbUrlFactory">
<property name="database" value="file:${hsqldb_database}" />
</bean>
<bean id="hsqldbDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" ref="hsqldbUrl" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="validationQuery" value="SELECT count(*) FROM INFORMATION_SCHEMA.SYSTEM_TABLES" />
<property name="initialSize" value="1" />
<property name="maxIdle" value="2" />
<property name="maxActive" value="5" />
</bean>
<bean id="conversionDao"
class="….HsqldbDAOImpl"
destroy-method="shutdown">
<property name="dataSource" ref="hsqldbDataSource" />
</bean>
</beans>
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.dao.DataIntegrityViolationException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Date;
/**
* DAO implementation backed by HSQLDB.
*
* @author Brian Lalor &lt;[email protected]&gt;
*/
public class HsqldbDAOImpl extends SimpleJdbcDaoSupport
{
// {{{ shutdown
/**
* Performs destroy-time deinitialization of the bean. Required to keep Hsqldb happy.
*/
public void shutdown() {
logger.debug("shutting down database");
getSimpleJdbcTemplate().update("shutdown");
}
// }}}
}
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import java.io.File;
/**
* Spring factory for Hsqldb URLs.
*
* @author Brian Lalor &lt;[email protected]&gt;
*/
public class HsqldbUrlFactory implements InitializingBean, FactoryBean
{
private Resource databaseRsrc;
private boolean scriptMustExist = false;
private String dbFilePath;
// {{{ setDatabase
public void setDatabase(final Resource database) {
this.databaseRsrc = database;
}
// }}}
// {{{ setScriptMustExist
public void setScriptMustExist(final boolean scriptMustExist) {
this.scriptMustExist = scriptMustExist;
}
// }}}
// {{{ afterPropertiesSet
/** {@inheritDoc} */
public void afterPropertiesSet() throws Exception {
dbFilePath = databaseRsrc.getFile().getAbsolutePath();
if (scriptMustExist) {
File dbf = new File(dbFilePath + ".script");
if (! dbf.exists()) {
throw new IllegalArgumentException(dbf.toString() + " does not exist");
}
}
}
// }}}
// {{{ getObject
/** {@inheritDoc} */
public Object getObject() throws Exception {
return String.format("jdbc:hsqldb:file:%s;ifexists=true", dbFilePath);
}
// }}}
// {{{ getObjectType
/** {@inheritDoc} */
public Class getObjectType() {
return String.class;
}
// }}}
// {{{ isSingleton
/** {@inheritDoc} */
public boolean isSingleton() {
return true;
}
// }}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment