Commit addc6a67 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

java code uses flyway to manage migration scripts

parent e74860da
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lcsb.mapviewer</groupId>
......@@ -75,7 +76,7 @@
<dependency>
<groupId>org.sbml.jsbml</groupId>
<artifactId>jsbml</artifactId>
<version>1.2</version>
<version>1.3.1</version>
<!-- for now we use lower version of log4j and this one introduce some
problem with tomcat loggin -->
<exclusions>
......@@ -91,6 +92,10 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
......
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
......@@ -17,6 +16,13 @@
<dependencies>
<!-- https://mvnrepository.com/artifact/org.flywaydb/flyway-core -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>
<!-- dependency from the MapViewer model -->
<dependency>
<groupId>lcsb.mapviewer</groupId>
......@@ -24,12 +30,12 @@
<version>1.0</version>
</dependency>
<!-- apache ftp -->
<dependency>
<!-- apache ftp -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
</dependency>
<!-- Hibernate -->
<dependency>
......
package lcsb.mapviewer.persist;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.flywaydb.core.Flyway;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ScriptException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class CustomDatabasePopulator implements DatabasePopulator {
Logger logger = Logger.getLogger(CustomDatabasePopulator.class);
private DataSource dataSource;
// public CustomDatabasePopulator(DataSource source) {
// logger.debug("Constructor");
// logger.debug(source);
// }
@Override
public void populate(Connection connection) throws SQLException, ScriptException {
logger.debug(dataSource);
ComboPooledDataSource source = (ComboPooledDataSource) dataSource;
String url = source.getJdbcUrl();
String user = source.getUser();
String password = source.getPassword();
Flyway flyway = new Flyway();
flyway.setDataSource(url, user, password);
flyway.baseline();
flyway.migrate();
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
......@@ -9,12 +9,17 @@ import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.log4j.Logger;
import org.flywaydb.core.Flyway;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.jmx.StatisticsService;
import org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import lcsb.mapviewer.common.exception.InvalidStateException;
/**
......@@ -74,8 +79,8 @@ public class DbUtils extends Observable {
* <ol>
* <li>when autoflushing is set to false, then the data consistency could be
* broken (even in the same thread/transaction)</li>,
* <li>we have to automatically take care of the flushing, therefore there
* might be some problems with long transactions.</li>
* <li>we have to automatically take care of the flushing, therefore there might
* be some problems with long transactions.</li>
* </ol>
*/
private static Map<Long, Boolean> autoFlushForThread = new HashMap<>();
......@@ -205,7 +210,8 @@ public class DbUtils extends Observable {
session.beginTransaction();
logger.debug("Session started: " + id);
if (statisticsService != null) {
logger.debug("Opened sessions: " + statisticsService.getSessionOpenCount() + ". Closed sessions: " + statisticsService.getSessionCloseCount());
logger.debug("Opened sessions: " + statisticsService.getSessionOpenCount() + ". Closed sessions: "
+ statisticsService.getSessionCloseCount());
}
Integer counter = -1;
synchronized (sessionForThread) {
......@@ -270,8 +276,8 @@ public class DbUtils extends Observable {
* Returns info if the flush is automatically done or not in current
* {@link Thread}.
*
* @return <code>true</code> if flush is automatically done,
* <code>false</code> otherwise
* @return <code>true</code> if flush is automatically done, <code>false</code>
* otherwise
* @see #autoFlushForThread
*/
public boolean isAutoFlush() {
......@@ -294,7 +300,7 @@ public class DbUtils extends Observable {
* Set autoFlush for current {@link Thread}.
*
* @param autoFlush
* the ne autoflush value
* the new autoflush value
* @see #autoFlushForThread
*/
public void setAutoFlush(boolean autoFlush) {
......@@ -303,5 +309,4 @@ public class DbUtils extends Observable {
autoFlushForThread.put(id, autoFlush);
}
}
}
......@@ -9,11 +9,15 @@
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder ignore-resource-not-found="true" location="classpath:db.properties,file:/etc/minerva/db.properties" />
<context:property-placeholder
ignore-resource-not-found="true"
location="classpath:db.properties,file:/etc/minerva/db.properties" />
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- Data Source Declaration -->
<bean id="DataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="${database.uri}" />
<property name="user" value="${database.username}" />
......@@ -23,4 +27,13 @@
<property name="minPoolSize" value="5" />
</bean>
<bean id="DbInit" class="lcsb.mapviewer.persist.CustomDatabasePopulator">
<property name="dataSource"><ref bean="DataSource"/></property>
</bean>
<bean id="startupScripts" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
<property name="dataSource"><ref bean="DataSource"/></property>
<property name="databasePopulator"><ref bean="DbInit"/></property>
</bean>
</beans>
Supports Markdown
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