为什么我不能关闭HibernateSessionFactory并重新打开它仍然有效?

我在关闭Hibernate Session Factory时遇到问题,我的应用程序允许用户重新创建数据库,所以当他们想要首先关闭hibernate会话工厂以释放数据库中的Hibernates时。

public static void closeFactory() { if(factory != null) { factory.close(); } } 

然后我等了几秒钟似乎有所帮助

  Thread.sleep(10000); 

然后我重新创建数据库,(FWIW常规hibernate和Envers – 我需要为要创建的Envers表创建工厂),然后再次关闭工厂。

 public static void recreateDatabase() { Configuration config; config = HibernateUtil.getInitializedConfigurationAndRebuildAuditTables(); new SchemaExport(config).create(true, true); factory = config.buildSessionFactory(); factory.close(); factory=null; } 

但是当我测试会话时:

 public static Session getSession() { if (factory == null) { createFactory(); } Session hibernateSession = factory.openSession(); return hibernateSession; } 

并尝试使用它来访问基于Hibernate的类(Song.class),它失败了抱怨

 org.hibernate.exception.SQLGrammarException: Table "SONG" not found; SQL statement: 

基本上它看不到任何表格。

我可以通过在重新创建表后关闭SessionFactory来解决此问题

 public static void recreateDatabase() { Configuration config; config = HibernateUtil.getInitializedConfigurationAndRebuildAuditTables(); new SchemaExport(config).create(true, true); factory = config.buildSessionFactory(); } 

但是这有两个问题

  1. 我不明白为什么我不能关闭和会议重新开工厂
  2. 我原本看这个代码是因为我偶尔会遇到Hibernate问题而且我希望能够重置会话工厂。

有人能解释一下吗?

注意

没有

factory = config.buildSessionFactory(); 线

在createDatabase()方法中创建表后我得到了

  ....... create index IDX_SONG_CHANGES_REPORTID on SongChanges (reportId) alter table Song_CoverArt add constraint FKE29AB716436A2867 foreign key (Song_recNo) references Song alter table Song_CoverArt add constraint FKE29AB716792380A foreign key (coverArts_id) references CoverArt Sep 10, 2014 2:03:45 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete 

而那条线,我得到了

 .............. create index IDX_SONG_CHANGES_REPORTID on SongChanges (reportId) alter table Song_CoverArt add constraint FKE29AB716436A2867 foreign key (Song_recNo) references Song alter table Song_CoverArt add constraint FKE29AB716792380A foreign key (coverArts_id) references CoverArt Sep 10, 2014 2:07:27 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Sep 10, 2014 2:07:27 PM org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator instantiateExplicitConnectionProvider INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider Sep 10, 2014 2:07:27 PM org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure INFO: HHH010002: C3P0 using driver: org.h2.Driver at URL: jdbc:h2:Database/songlayer;FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE;CACHE_SIZE=50000 Sep 10, 2014 2:07:27 PM org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure INFO: HHH000046: Connection properties: {user=jaikoz, password=****} Sep 10, 2014 2:07:27 PM org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure INFO: HHH000006: Autocommit mode: false Sep 10, 2014 2:07:27 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@2faff047 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@8f17d9e0 [ acquireIncrement -> 3, acquireRetryAttempts -> 10, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge15494qjod2z19iq4cc|2925bf5b, idleConnectionTestPeriod -> 3000, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 300, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 0, maxStatementsPerConnection -> 50, minPoolSize -> 20, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@3f342714 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge15494qjod2z19iq4cc|45c7e403, jdbcUrl -> jdbc:h2:Database/songlayer;FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE;CACHE_SIZE=50000, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge15494qjod2z19iq4cc|710f4dc7, numHelperThreads -> 10 ] Sep 10, 2014 2:07:27 PM org.hibernate.dialect.Dialect  INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect Sep 10, 2014 2:07:27 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 Sep 10, 2014 2:07:27 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) Sep 10, 2014 2:07:27 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory  INFO: HHH000397: Using ASTQueryTranslatorFactory Sep 10, 2014 2:07:28 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000227: Running hbm2ddl schema export Sep 10, 2014 2:07:28 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: HHH000389: Unsuccessful: alter table Song_AUD drop constraint FK5F61F486DF74E053 Sep 10, 2014 2:07:28 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: Constraint "FK5F61F486DF74E053" not found; SQL statement: alter table Song_AUD drop constraint FK5F61F486DF74E053 [90057-166] Sep 10, 2014 2:07:28 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: HHH000389: Unsuccessful: alter table Song_CoverArt_AUD drop constraint FK19C969E7DF74E053 Sep 10, 2014 2:07:28 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: Constraint "FK19C969E7DF74E053" not found; SQL statement: alter table Song_CoverArt_AUD drop constraint FK19C969E7DF74E053 [90057-166] Sep 10, 2014 2:07:29 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete 

我不明白为什么,但它是必要的。