在应用程序关闭时删除没有后备域类的表

时间:2019-08-13 07:55:06

标签: hibernate spring-boot grails gorm

我想在应用程序关闭时删除表,该表是在应用程序启动期间即时创建的。没有Domain类。

我在destroy中使用了Bootstrap.groovy闭包,就像这样

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()方法看起来像

void drop(List<String> tables) {
    Session session = sessionFactory.currentSession // Getting exception on this line
    tables.each {
        session.createSQLQuery("drop table if exists $it").executeUpdate()
    }
}

我得到了

  

运行Bootstrap销毁方法时发生错误:未找到会话   当前线程

     

org.hibernate.HibernateException:未找到当前线程的会话           在org.grails.orm.hibernate.GrailsS​​essionContext.currentSession(GrailsS​​essionContext.java:116)           在org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:688)           在sun.reflect.GeneratedMethodAccessor492.invoke(未知来源)           在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           在java.lang.reflect.Method.invoke(Method.java:498)           在org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)

注意:Grails,3.2.8

1 个答案:

答案 0 :(得分:0)

我使用了一种解决方法;像下面一样移动了init中的那个位

def init = {
    String dbCreate = Holders.grailsApplication.config.getProperty('dataSource.dbCreate', String)
    if(dbCreate == 'create-drop') 
        baseSeqService.drop(customTables)
    baseSeqService.create(customTables)    
    ...
}