我在关闭Hibernate会话工厂时遇到问题,我的应用程序允许用户重新创建数据库,所以当他们想要首先关闭hibernate会话工厂以释放数据库中的Hibernates时。 / p>
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();
}
然而,这个
存在两个问题有人可以向我解释一下吗?
注意
没有
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 <init>
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 <init>
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
我不明白为什么,但需要它。