forked from Archives/Athou_commafeed
no casting of sessionfactory necessary this way
This commit is contained in:
@@ -1,16 +1,12 @@
|
|||||||
package com.commafeed.backend.service;
|
package com.commafeed.backend.service;
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;
|
|
||||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
|
||||||
import org.hibernate.internal.SessionFactoryImpl;
|
|
||||||
|
|
||||||
import com.commafeed.CommaFeedApplication;
|
import com.commafeed.CommaFeedApplication;
|
||||||
import com.commafeed.CommaFeedConfiguration;
|
import com.commafeed.CommaFeedConfiguration;
|
||||||
@@ -31,7 +27,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor(onConstructor = @__({ @Inject }) )
|
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
|
||||||
@Singleton
|
@Singleton
|
||||||
public class StartupService implements Managed {
|
public class StartupService implements Managed {
|
||||||
|
|
||||||
@@ -50,17 +46,10 @@ public class StartupService implements Managed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateSchema() {
|
private void updateSchema() {
|
||||||
try {
|
Session session = sessionFactory.openSession();
|
||||||
Connection connection = null;
|
session.doWork(connection -> {
|
||||||
try {
|
try {
|
||||||
Thread currentThread = Thread.currentThread();
|
|
||||||
ClassLoader classLoader = currentThread.getContextClassLoader();
|
|
||||||
ResourceAccessor accessor = new ClassLoaderResourceAccessor(classLoader);
|
|
||||||
|
|
||||||
DataSource dataSource = getDataSource(sessionFactory);
|
|
||||||
connection = dataSource.getConnection();
|
|
||||||
JdbcConnection jdbcConnection = new JdbcConnection(connection);
|
JdbcConnection jdbcConnection = new JdbcConnection(connection);
|
||||||
|
|
||||||
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
|
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
|
||||||
|
|
||||||
if (database instanceof PostgresDatabase) {
|
if (database instanceof PostgresDatabase) {
|
||||||
@@ -73,17 +62,14 @@ public class StartupService implements Managed {
|
|||||||
database.setConnection(jdbcConnection);
|
database.setConnection(jdbcConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ResourceAccessor accessor = new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
|
||||||
Liquibase liq = new Liquibase("migrations.xml", accessor, database);
|
Liquibase liq = new Liquibase("migrations.xml", accessor, database);
|
||||||
liq.update("prod");
|
liq.update("prod");
|
||||||
} finally {
|
} catch (Exception e) {
|
||||||
if (connection != null) {
|
throw new RuntimeException(e);
|
||||||
connection.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
} catch (Exception e) {
|
session.close();
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialData() {
|
private void initialData() {
|
||||||
@@ -103,15 +89,4 @@ public class StartupService implements Managed {
|
|||||||
public void stop() throws Exception {
|
public void stop() throws Exception {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DataSource getDataSource(SessionFactory sessionFactory) {
|
|
||||||
if (sessionFactory instanceof SessionFactoryImpl) {
|
|
||||||
ConnectionProvider cp = ((SessionFactoryImpl) sessionFactory).getConnectionProvider();
|
|
||||||
if (cp instanceof DatasourceConnectionProviderImpl) {
|
|
||||||
return ((DatasourceConnectionProviderImpl) cp).getDataSource();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user