I would like to drop tables upon application shutdown, which I created on-the-fly during startup of the application. There are no Domain
classes for those.
I used destroy
closure available in in Bootstrap.groovy
, like this
def someService
...
...
def destroy = {
String dbCreate = Holders.grailsApplication.config.getProperty('dataSource.dbCreate', String)
if(dbCreate == 'create-drop')
someService.drop(customTables) // customTables is a List of names
}
drop()
method in service looks like,
void drop(List<String> tables) {
Session session = sessionFactory.currentSession // Getting exception on this line
tables.each {
session.createSQLQuery("drop table if exists $it").executeUpdate()
}
}
I'm getting,
Error occurred running Bootstrap destroy method: No Session found for current thread
org.hibernate.HibernateException: No Session found for current thread at org.grails.orm.hibernate.GrailsSessionContext.currentSession(GrailsSessionContext.java:116) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:688) at sun.reflect.GeneratedMethodAccessor492.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
Note: Grails, 3.2.8
I used a kind of work around; moved that bit in init
, like below
def init = {
String dbCreate = Holders.grailsApplication.config.getProperty('dataSource.dbCreate', String)
if(dbCreate == 'create-drop')
baseSeqService.drop(customTables)
baseSeqService.create(customTables)
...
}