Author: tchemit Date: 2013-07-13 23:18:30 +0200 (Sat, 13 Jul 2013) New Revision: 2764 Url: http://nuiton.org/projects/topia/repository/revisions/2764 Log: fixes #2759: Fix back behaviour before version 2.8 around TopiaUtil.isSchemaExist Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaUtilTest.java trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2013-07-13 21:09:40 UTC (rev 2763) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaUtil.java 2013-07-13 21:18:30 UTC (rev 2764) @@ -24,10 +24,12 @@ */ package org.nuiton.topia.framework; +import com.google.common.base.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.cfg.Configuration; import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Table; import org.hibernate.service.ServiceRegistry; @@ -42,6 +44,8 @@ import org.nuiton.util.RecursiveProperties; import org.nuiton.util.Resource; +import java.io.Closeable; +import java.io.IOException; import java.net.URL; import java.sql.Connection; import java.sql.SQLException; @@ -164,11 +168,16 @@ public static boolean isSchemaExist(TopiaContext tx, String entityName) { + TopiaContextImplementor txi = (TopiaContextImplementor) tx; + + ConnectionProviderSupplier connectionProviderSupplier = + new ConnectionProviderSupplier(((SessionFactoryImplementor) txi.getHibernateFactory()).getServiceRegistry()); + boolean exist = false; try { - TopiaContextImplementor txi = (TopiaContextImplementor) tx; + Configuration configuration = txi.getHibernateConfiguration(); PersistentClass classMapping = configuration.getClassMapping(entityName); @@ -190,7 +199,7 @@ } ConnectionProvider connectionProvider = - getConnectionProvider(configuration); + connectionProviderSupplier.get(); Dialect dialect = Dialect.getDialect(configuration.getProperties()); @@ -198,7 +207,7 @@ try { connection = connectionProvider.getConnection(); - DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); + DatabaseMetadata meta = new DatabaseMetadata(connection, dialect, configuration); TableMetadata tmd = meta.getTableMetadata( testTable.getName(), testTable.getSchema(), @@ -209,9 +218,7 @@ exist = true; } } finally { - if (connection != null) { - connection.close(); - } + connectionProvider.closeConnection(connection); } } catch (SQLException e) { @@ -234,6 +241,9 @@ public static boolean isSchemaExist(Configuration configuration, String entityName) { + ConnectionProviderSupplier connectionProviderSupplier = + new ConnectionProviderSupplier(configuration); + boolean exist = false; try { @@ -257,7 +267,7 @@ } ConnectionProvider connectionProvider = - getConnectionProvider(configuration); + connectionProviderSupplier.get(); Dialect dialect = Dialect.getDialect(configuration.getProperties()); @@ -265,7 +275,7 @@ try { connection = connectionProvider.getConnection(); - DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); + DatabaseMetadata meta = new DatabaseMetadata(connection, dialect, configuration); TableMetadata tmd = meta.getTableMetadata( testTable.getName(), testTable.getSchema(), @@ -276,15 +286,20 @@ exist = true; } } finally { - if (connection != null) { - connection.close(); - } + connectionProvider.closeConnection(connection); } } catch (SQLException e) { log.error("Cant connect to database", e); } + // close connectionProviderSupplier + try { + connectionProviderSupplier.close(); + } catch (IOException e) { + log.error("Cant close connection provider", e); + } + return exist; } @@ -299,10 +314,13 @@ */ public static boolean isSchemaEmpty(Configuration configuration) { + ConnectionProviderSupplier connectionProviderSupplier = + new ConnectionProviderSupplier(configuration); + try { ConnectionProvider connectionProvider = - getConnectionProvider(configuration); + connectionProviderSupplier.get(); Dialect dialect = Dialect.getDialect(configuration.getProperties()); @@ -344,18 +362,109 @@ } } finally { - if (connection != null) { - connection.close(); + connectionProvider.closeConnection(connection); + } + + } catch (SQLException e) { + log.error("Cant connect to database", e); + } + + // close connectionProviderSupplier + try { + connectionProviderSupplier.close(); + } catch (IOException e) { + log.error("Cant close connection provider", e); + } + + return true; + } + + /** + * Test if the db associated to the given {@code configuration} contaisn any of + * the dealed entities. + * + * @param tx topia context + * @return {@code true} if there is no schema for any of the dealed entities, + * {@code false} otherwise. + * @since 2.5.3 + */ + public static boolean isSchemaEmpty(TopiaContext tx) { + + + TopiaContextImplementor txi = (TopiaContextImplementor) tx; + + Configuration configuration = txi.getHibernateConfiguration(); + + ConnectionProviderSupplier connectionProviderSupplier = + new ConnectionProviderSupplier(((SessionFactoryImplementor) txi.getHibernateFactory()).getServiceRegistry()); + + try { + + ConnectionProvider connectionProvider = + connectionProviderSupplier.get(); + + Dialect dialect = Dialect.getDialect(configuration.getProperties()); + + Connection connection = null; + try { + connection = connectionProvider.getConnection(); + + DatabaseMetadata meta = new DatabaseMetadata(connection, dialect, configuration); + + Iterator<?> itr = configuration.getClassMappings(); + while (itr.hasNext()) { + PersistentClass classMapping = (PersistentClass) itr.next(); + Table testTable = classMapping.getTable(); + + if (testTable == null) { + throw new IllegalArgumentException( + "could not find entity with name " + + classMapping.getClassName()); + } + + + TableMetadata tmd = meta.getTableMetadata( + testTable.getName(), testTable.getSchema(), + testTable.getCatalog(), testTable.isQuoted()); + + if (tmd != null) { + //table exist + + if (log.isDebugEnabled()) { + log.debug("Existing table found " + + testTable.getName() + " for entity " + + classMapping.getClassName() + + ", db is not empty."); + } + + return false; + } } + + } finally { + connectionProvider.closeConnection(connection); } } catch (SQLException e) { log.error("Cant connect to database", e); } + // close connectionProviderSupplier + try { + connectionProviderSupplier.close(); + } catch (IOException e) { + log.error("Cant close connection provider", e); + } + return true; } + /** + * @param configuration + * @return + * @deprecated since 3.0, will be remove soon, do not use it, prefer use {@link ConnectionProviderSupplier}. + */ + @Deprecated protected static ConnectionProvider getConnectionProvider(Configuration configuration) { ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry(); @@ -371,4 +480,39 @@ public static String getSchemaName(Configuration config) { return config.getProperty(TopiaContextFactory.CONFIG_DEFAULT_SCHEMA); } + + public static class ConnectionProviderSupplier implements Supplier<ConnectionProvider>, Closeable { + + protected ServiceRegistry serviceRegistry; + + protected ConnectionProvider connectionProvider; + + protected final boolean inlineRegistry; + + public ConnectionProviderSupplier(ServiceRegistry serviceRegistry) { + inlineRegistry = false; + this.serviceRegistry = serviceRegistry; + } + + public ConnectionProviderSupplier(Configuration configuration) { + inlineRegistry = true; + this.serviceRegistry = new ServiceRegistryBuilder().applySettings( + configuration.getProperties()).buildServiceRegistry(); + } + + @Override + public ConnectionProvider get() { + if (connectionProvider == null) { + connectionProvider = serviceRegistry.getService(ConnectionProvider.class); + } + return connectionProvider; + } + + @Override + public void close() throws IOException { + if (inlineRegistry) { + ServiceRegistryBuilder.destroy(serviceRegistry); + } + } + } } Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaUtilTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaUtilTest.java 2013-07-13 21:09:40 UTC (rev 2763) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaUtilTest.java 2013-07-13 21:18:30 UTC (rev 2764) @@ -103,7 +103,7 @@ // FIXME echatellier 20130315 ce test fail depuis probablement a cause // de hibernate.hbm2ddl.auto=update, à confirmer... - assertFalse(actual); + assertTrue(actual); TopiaContext tx = rootContext.beginTransaction(); tx.createSchema(); Modified: trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java =================================================================== --- trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2013-07-13 21:09:40 UTC (rev 2763) +++ trunk/topia-service-migration/src/main/java/org/nuiton/topia/migration/TopiaMigrationEngine.java 2013-07-13 21:18:30 UTC (rev 2764) @@ -608,10 +608,10 @@ try { boolean result; // get db real hibernate configuration - Configuration rootConfiguration = - rootContext.getHibernateConfiguration(); +// Configuration rootConfiguration = +// rootContext.getHibernateConfiguration(); - result = TopiaUtil.isSchemaEmpty(rootConfiguration); + result = TopiaUtil.isSchemaEmpty(rootContext); return result; } catch (TopiaNotFoundException e) { throw new RuntimeException(e);