java - Why cant I close HibernateSessionFactory and reopen it and it still work? -


i'm having problem closing hibernate session factory, application allows user recreate database, when want firstly close hibernate session factory free hibernates grip on database.

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

then wait few seconds seems help

  thread.sleep(10000); 

then recreate database, (fwiw regular hibernate , envers - need create factory envers tables created), , close factory down again.

public static void recreatedatabase() {     configuration config;     config =             hibernateutil.getinitializedconfigurationandrebuildaudittables();     new schemaexport(config).create(true, true);     factory = config.buildsessionfactory();     factory.close();     factory=null; } 

but when test session with:

public static session getsession() {     if (factory == null)     {        createfactory();     }     session hibernatesession = factory.opensession();     return hibernatesession; } 

and try use access hibernate based class (song.class )it fails complaining

org.hibernate.exception.sqlgrammarexception: table "song" not found; sql statement: 

basically cant see of tables.

i can stop problem not closing sessionfactory after recreating table

public static void recreatedatabase() {     configuration config;     config =             hibernateutil.getinitializedconfigurationandrebuildaudittables();     new schemaexport(config).create(true, true);     factory = config.buildsessionfactory(); } 

however there 2 problems this

  1. i don't understand why cant close , session reopen factory
  2. i looking code because getting occasional hibernate problems , wanted able reset session factory.

can explain me please ?

note

without

factory = config.buildsessionfactory(); line

in createdatabase() methodafter tables created

  .......     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 

whereas line get

..............  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 @ 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 jdbc driver reported jdbc version [3] less 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 

i dont understand why, needed.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -