我想在应用程序关闭时删除表,这是我在应用程序启动期间即时创建的。这些没有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()
}
}
我越来越,
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)
注意:Grails 3.2.8
最佳答案
我使用了一种解决方法;将其移动到init
中,如下所示
def init = {
String dbCreate = Holders.grailsApplication.config.getProperty('dataSource.dbCreate', String)
if(dbCreate == 'create-drop')
baseSeqService.drop(customTables)
baseSeqService.create(customTables)
...
}
关于hibernate - 在应用程序关闭时删除没有后备域类的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473427/