我有一个带有spring-boot-data-jpa的spring boot应用程序。
一切正常:会话管理,帐户管理,这些对象之间的关系等...
我想添加一个新实体来记录用户的帐户操作,但是一旦我尝试保存它,它就没有。请记住,春季会议和帐户管理工作中的所有其他任务都已实施,并且已完全相同。 (春季会议之一是自动实施。
@Entity
@Table(indexes = [Index(columnList = "username")])
open class Event {
@Id
@GeneratedValue
var id: UUID? = null
@Enumerated(EnumType.STRING)
lateinit var type: EventType
var username: String = ""
var addr: String = ""
var port: Int = 0
var date: Date = Date()
}
enum class EventType {
LOGIN, LOGOUT, LOGIN_FAIL, CERT_GEN
}
存储库:
@Repository
interface EventRepository: JpaRepository<Event, UUID>
服务:
interface IEventService {
fun onLogin(request: HttpServletRequest, username: String)
fun onLogout(request: HttpServletRequest, username: String)
fun onLoginFail(request: HttpServletRequest, username: String)
fun onCert(request: HttpServletRequest, username: String)
}
展示:
@Service
@Transactional
open class EventService : IEventService {
@Autowired
private lateinit var eventRepository: EventRepository
override fun onLogin(request: HttpServletRequest, username: String) {
val evt = Event()
evt.addr = request.remoteAddr
evt.port = request.remotePort
evt.username = username
evt.type = EventType.LOGIN
eventRepository.save(evt)
}
... same with different EventType
这是来自事务性和休眠状态的事务的跟踪日志。
2018-07-01 14:59:06.685 TRACE 3128 --- [nio-8080-exec-3] o.s.t.i.TransactionInterceptor : Getting transaction for [com.acme.services.impl.EventService.onLogout]
2018-07-01 14:59:06.685 TRACE 3128 --- [nio-8080-exec-3] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2018-07-01 14:59:06.685 TRACE 3128 --- [nio-8080-exec-3] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null, owner=org.hibernate.jpa.internal.EntityManagerImpl@7f512e36
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.internal.SessionImpl : Opened session at timestamp: 15304499466
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.internal.SessionImpl : Setting flush mode to: AUTO
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.internal.SessionImpl : Setting cache mode to: NORMAL
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.e.i.AbstractSaveEventListener : Transient instance of: com.acme.dbo.Event
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.e.i.DefaultPersistEventListener : Saving transient instance
2018-07-01 14:59:06.686 DEBUG 3128 --- [nio-8080-exec-3] o.h.e.i.AbstractSaveEventListener : Generated identifier: b3619aa5-af5c-4552-a092-187d0d16e94b, using strategy: org.hibernate.id.UUIDGenerator
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.e.i.AbstractSaveEventListener : Saving [com.acme.dbo.Event#b3619aa5-af5c-4552-a092-187d0d16e94b]
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.engine.spi.ActionQueue : Adding an EntityInsertAction for [com.acme.dbo.Event] object
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.engine.spi.ActionQueue : Adding insert with no non-nullable, transient entities: [EntityInsertAction[com.acme.dbo.Event#b3619aa5-af5c-4552-a092-187d0d16e94b]]
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.engine.spi.ActionQueue : Adding resolved non-early insert action.
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.s.t.i.TransactionInterceptor : Completing transaction for [com.acme.services.impl.EventService.onLogout]
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] org.hibernate.internal.SessionImpl : Closing session
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@14babdc8]
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2018-07-01 14:59:06.686 TRACE 3128 --- [nio-8080-exec-3] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
2018-07-01 14:59:06.699 TRACE 3128 --- [nio-8080-exec-5] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null, owner=org.hibernate.jpa.internal.EntityManagerImpl@15a4558c
2018-07-01 14:59:06.699 TRACE 3128 --- [nio-8080-exec-5] org.hibernate.internal.SessionImpl : Opened session at timestamp: 15304499466
2018-07-01 14:59:06.699 TRACE 3128 --- [nio-8080-exec-5] org.hibernate.internal.SessionImpl : Setting flush mode to: AUTO
2018-07-01 14:59:06.699 TRACE 3128 --- [nio-8080-exec-5] org.hibernate.internal.SessionImpl : Setting cache mode to: NORMAL
2018-07-01 14:59:06.747 TRACE 3128 --- [nio-8080-exec-5] org.hibernate.internal.SessionImpl : Closing session
2018-07-01 14:59:06.747 TRACE 3128 --- [nio-8080-exec-5] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@610769c]
2018-07-01 14:59:06.747 TRACE 3128 --- [nio-8080-exec-5] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2018-07-01 14:59:06.747 TRACE 3128 --- [nio-8080-exec-5] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2018-07-01 14:59:06.747 TRACE 3128 --- [nio-8080-exec-5] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
我在此类处理程序中使用这些服务,并使用http.logout().addLogoutHandler(logoutHandler)
将其添加到HttpSecurity中:
@Component
open class AcmeLogoutHandler : LogoutHandler {
@Autowired
private lateinit var service: IEventService
override fun logout(request: HttpServletRequest, response: HttpServletResponse, authentication: Authentication?) {
service.onLogout(request, authentication?.name ?: "")
}
}
我非常想知道这个问题...没有错误,但是它不起作用,我尝试做的任何事情似乎都失败了。
一件值得注意的事情是,当我使用eventRepository.saveAndFlush(evt)
而不是eventRepository.save(evt)
时,它会抛出javax.persistence.TransactionRequiredException: no transaction is in progress
尽管日志清楚显示当前正在加载事务。也许这是问题的关键。
更新:
我找到了一个可以使用该确切代码的案例。简单地运行它就是传统的端点/服务层。问题肯定是从处理程序访问数据库。