gpt4 book ai didi

java - bitronix- 无法定位当前的 JTA 事务

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:17 29 4
gpt4 key购买 nike

您好,我有一个使用 spring 和 hibernate 的 Java web 应用程序。

我有一个名为Role 的模型类。对于所有模型类,都有通用的 GenericDao.java

public interface GenericDao<T>{
public void insert(T transientInstance);
public T findById(Class<?> clazz, Serializable id);
}

GenericHibernateDao.java

@Repository
public class GenericHibernateDao<T extends Serializable>
implements GenericDao<T>{

@Resource
protected SessionFactory sessionFactory;

@Override
public void insert(T transientInstance) {
sessionFactory.getCurrentSession().persist(transientInstance);
}
@SuppressWarnings("unchecked")
@Override
public T findById(Class<?> clazz, Serializable id) {
return (T) sessionFactory.getCurrentSession().get(clazz, id);
}
}

RoleService.java

public interface RoleService {
public void insert(Role role);
public Role findById(Integer id);
}

并且有它的实现 RoleServiceImpl.java

@Service
public class RoleServiceImpl implements RoleService {

@Autowired
private GenericDao<Role> roleDao;

@Override
public void insert(Role role) {
roleDao.insert(role);
}

@Override
public Role findById(Integer id) {
return roleDao.findById(Role.class, id);
}
}

我有一个 Controller 调用此 findbyid 方法并传递一个值 - RegisterController.java

@Controller
public class UserRegisterController {

@Autowired
public RoleService roleService;

@RequestMapping(value = "/register", method = RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void registerUser(@RequestBody User user)
{
Role role=new Role();
role=roleService.findById(1);
System.out.println("Saved");
}
}

最后我的spring applicationContext.xml

<bean id="dataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource"
init-method="init" destroy-method="close">
<property name="className"
value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="uniqueName" value="ocr/DocumentDatabase" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="5" />
<property name="automaticEnlistingEnabled" value="true" />
<property name="allowLocalTransactions" value="true" />
<property name="useTmJoin" value="true" />
<property name="shareTransactionConnections" value="true" />
<property name="driverProperties">
<props>
<prop key="url">${jdbc.url}</prop>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
</props>
</property>
</bean>
<bean id="bitronixTransactionManager" factory-method="getTransactionManager"
class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig, dataSource"
destroy-method="shutdown" />

<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="bitronixTransactionManager" />
<property name="userTransaction" ref="bitronixTransactionManager" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource" ref="dataSource" />
<property name="jtaTransactionManager" ref="transactionManager"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
</props>
</property>
</bean>

我在 tomcat 中运行这个应用程序。它给出了以下错误。

SEVERE: Servlet.service() for servlet [myappdemo] in context with path [/myappdemo] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: Unable to locate current JTA transaction] with root cause
org.hibernate.HibernateException: Unable to locate current JTA transaction
at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:88)
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:90)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980)
at com.myapp.myappdemo.dao.hibernate.GenericHibernateDao.findById(GenericHibernateDao.java:46)
at com.myapp.myappdemo.dao.hibernate.GenericHibernateDao.findById(GenericHibernateDao.java:15)
at com.myapp.myappdemo.service.impl.RoleServiceImpl.findById(RoleServiceImpl.java:38)
at com.myapp.myappdemo.web.UserRegisterController.registerUser(UserRegisterController.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

当我调用 roleService.findById(1);

时会发生这种情况

为什么会这样?我该如何解决?

我正在使用 spring 3.2.1.RELEASE,Hibernate 4.2.0.Final,btm 2.1.3

最佳答案

sessionFactory 被配置为使用 JtaTransactionManager。当您这样做时,hibernate 足够智能,可以查看当前的事务资源以找到 session 。

您的 spring 配置允许您使用注释来进行事务划分。

您的事务管理器似乎配置正确。

所以,一切看起来都很好。唯一缺少的部分是交易注释!

只需在您的服务层添加@Transactional:

@Override
@Transactional
public Role findById(Integer id) {
return roleDao.findById(Role.class, id);
}

(你也可以把这个注解放在服务类上)

关于java - bitronix- 无法定位当前的 JTA 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20967784/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com