"No transaction is in progress" 遵循 Grails Spring 安全核心教程时出错
"No transaction is in progress" error while following Grails Spring Security Core tutorial
我正在学习 Grails Spring 安全核心教程 in their documentation。当我到达 24.1.5.5 时,问题就开始了。我被指示将 Bootstrap.groovy 修改为以下内容,然后 运行 应用程序:
package com.mycompany.myapp
class BootStrap {
def init = {
def adminRole = new Role(authority: 'ROLE_ADMIN').save()
def testUser = new User(username: 'me', password: 'password').save()
UserRole.create testUser, adminRole
UserRole.withSession {
it.flush()
it.clear()
}
assert User.count() == 1
assert Role.count() == 1
assert UserRole.count() == 1
}
}
运行 应用程序给我这个错误:
2020-03-31 15:46:19.294 ERROR --- [ restartedMain]
o.s.boot.SpringApplication
: Application run failed
javax.persistence.TransactionRequiredException: no transaction is in
progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl
.java:3586)
我尝试使用@Transactional 来解决这个问题,但它对错误没有影响。将 Bootstrap.groovy 恢复为默认值允许应用程序正常 运行。本教程缺少(或不正确)什么导致它失败?
文档已过时。 Hibernate 5.2+ 现在需要写操作的事务。添加 @Transactional 不起作用,因为 init
是一个闭包并且注释适用于方法或 类。
在 bootstrap 中创建一个新方法并向其添加事务。然后从您的 init 闭包中调用它。例如。
class BootStrap {
def init = {
addTestUsers()
}
@Transactional
void addTestUsers() {
def adminRole = new Role(authority: 'ROLE_ADMIN').save()
def testUser = new User(username: 'me', password: 'password').save()
UserRole.create testUser, adminRole
UserRole.withSession {
it.flush()
it.clear()
}
assert User.count() == 1
assert Role.count() == 1
assert UserRole.count() == 1
}
}
您也可以尝试 https://github.com/grails/grails-core/issues/11376 中的解决方案,其中说您可以将配置
添加到 application.yml
hibernate:
allow_update_outside_transaction: true
我正在学习 Grails Spring 安全核心教程 in their documentation。当我到达 24.1.5.5 时,问题就开始了。我被指示将 Bootstrap.groovy 修改为以下内容,然后 运行 应用程序:
package com.mycompany.myapp
class BootStrap {
def init = {
def adminRole = new Role(authority: 'ROLE_ADMIN').save()
def testUser = new User(username: 'me', password: 'password').save()
UserRole.create testUser, adminRole
UserRole.withSession {
it.flush()
it.clear()
}
assert User.count() == 1
assert Role.count() == 1
assert UserRole.count() == 1
}
}
运行 应用程序给我这个错误:
2020-03-31 15:46:19.294 ERROR --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl .java:3586)
我尝试使用@Transactional 来解决这个问题,但它对错误没有影响。将 Bootstrap.groovy 恢复为默认值允许应用程序正常 运行。本教程缺少(或不正确)什么导致它失败?
文档已过时。 Hibernate 5.2+ 现在需要写操作的事务。添加 @Transactional 不起作用,因为 init
是一个闭包并且注释适用于方法或 类。
在 bootstrap 中创建一个新方法并向其添加事务。然后从您的 init 闭包中调用它。例如。
class BootStrap {
def init = {
addTestUsers()
}
@Transactional
void addTestUsers() {
def adminRole = new Role(authority: 'ROLE_ADMIN').save()
def testUser = new User(username: 'me', password: 'password').save()
UserRole.create testUser, adminRole
UserRole.withSession {
it.flush()
it.clear()
}
assert User.count() == 1
assert Role.count() == 1
assert UserRole.count() == 1
}
}
您也可以尝试 https://github.com/grails/grails-core/issues/11376 中的解决方案,其中说您可以将配置
添加到 application.ymlhibernate:
allow_update_outside_transaction: true