Could not obtain transaction-synchronized Session problem (fixed)
以下程式最大問題是 createRandomAbstract 没有加 @Override @Transactional
修改以下CODE 便能解決問題
@Override
@Transactional
public void createRandomAbstract(int theId) {
Book book=bookDao.getBook(theId);
Abstractpara abstractpara=new Abstractpara();
abstractpara.setContent("test 2");
abstractpara.setLanguage("English");
abstractpara.setBook(book);
book.addAbstractpara(abstractpara);
book=bookDao.saveBook(book);
}
======================================
package com.book.gen.domain.services.aggregates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.book.gen.domain.aggregates.repositories.BookAbstractparaDao;
import com.book.gen.domain.models.*;
import com.book.gen.domain.models.repositories.AbstractparaDao;
import com.book.gen.domain.models.repositories.BookDao;
@Service
public class BookAbstractparaServiceImpl implements BookAbstractparaService {
// need to inject book Dao
@Autowired
private BookDao bookDao;
// need to inject abstractpara Dao
@Autowired
private AbstractparaDao abstractparaDao;
@Autowired
private BookAbstractparaDao bookAbstractparaRepository;
public void createRandomAbstract(int theId) {
Book book=bookDao.getBook(theId);
Abstractpara abstractpara=new Abstractpara();
abstractpara.setContent("test 2");
abstractpara.setLanguage("English");
abstractpara.setBook(book);
book.addAbstractpara(abstractpara);
book=bookDao.saveBook(book);
}
}
Type Exception Report
Message Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) javax.servlet.http.HttpServlet.service(HttpServlet.java:529) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) javax.servlet.http.HttpServlet.service(HttpServlet.java:623) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Root Cause
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:142) org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:454) com.book.gen.domain.models.repositories.BookDaoImpl.getBook(BookDaoImpl.java:87) com.book.gen.domain.services.aggregates.BookAbstractparaServiceImpl.createRandomAbstract(BookAbstractparaServiceImpl.java:24) com.book.gen.controllers.BookControllerImpl.bookgridviewdetail3(BookControllerImpl.java:327) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.base/java.lang.reflect.Method.invoke(Method.java:568) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) javax.servlet.http.HttpServlet.service(HttpServlet.java:529) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) javax.servlet.http.HttpServlet.service(HttpServlet.java:623) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Note The full stack trace of the root cause is available in the server logs.
Comments
Post a Comment