【发布时间】:2014-10-29 08:33:41
【问题描述】:
我想将我的axis2项目与spring集成。我设法按照本指南加载了一个 spring applicationContext。
简短
这是我的axis2版本服务:
public class VersionService extends MyappService{
private static final Logger log = Logger.getLogger(VersionService.class);
@Autowired
NewUserMyappDAO newUserMyappDAO;
public Response getResponse(){
Response response = new Response();
UserMyapp ub = getTransaction();
return response;
}
@Transactional
public UserMyapp getTransaction(){
return newUserMyappDAO.findById(13);
}
}
问题:当axis调用getResponse()方法时,dao设法获得注入的sessionFactory(和hibernate会话),但是当@Transactional在该方法之上使用时,之前没有打开任何事务。这就是为什么我得到:
Caused by: org.hibernate.HibernateException: get is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at $Proxy45.get(Unknown Source)
at com.myapp.framework.model.dao.NewMyappDAO.findById(NewMyappDAO.java:35)
at com.myapp.ws.version.VersionService.getTransaction(VersionService.java:127)
at com.myapp.ws.version.VersionService.getResponse(VersionService.java:119)
我想要的是有一个 getTransaction() 方法,它可以自动启动事务 (Hibernate session.beginTransaction()) 并在其中出现问题时回滚。
我也试过删除
<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop>
但在这种情况下,spring 无法加载 userMyAppDAO,因为 org.hibernate.HibernateException: No Session found for current thread
详情
我的 applicationContext.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:annotation-config />
<context:component-scan base-package="com.myapp.framework.model.dao"></context:component-scan>
<!-- Axis2 Web Service, but to Spring, its just another bean that has dependencies -->
<bean id="versionService" class="com.myapp.ws.version.VersionService"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://db.myapp.com:3307/MyappAPI" />
<property name="username" value="myapp" />
<property name="password" value="myappculomyapp" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.myapp.framework.model.dao</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.globally_quoted_identifiers">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
这是 DAO 及其超类:
@Repository
public class NewUserMyappDAO extends NewMyappDAO<UserMyapp, Integer>{
@Autowired
public NewUserMyappDAO(SessionFactory sessionFactory){
super(UserMyapp.class, sessionFactory);
}
}
@Repository
public abstract class NewMyAppDAO<E, ID extends Serializable>
implements IMyAppDAO<E, ID> {
private final Class<E> entityClass;
protected Session session;
public NewMyAppDAO(Class<E> entityClass, SessionFactory sessionFactory) {
this.entityClass = entityClass;
this.session = sessionFactory.getCurrentSession();
}
public Class<E> getEntityClass() {
return entityClass;
}
@SuppressWarnings({ "unchecked" })
public E findById(ID id) {
Object obj = null;
try {
obj = session.get(getEntityClass(), id);
} catch(ObjectNotFoundException e){
return null;
}
return (E) obj;
}
编辑
vp8106 留下的答案似乎是正确的,但我试图后退一步,试图以编程方式管理事务。我所做的是在 getResponse() 方法中显式使用 beginTransaction()、commitTransaction()、rollbackTransaction() 和 close()。即使 sessionFactory 对象是一个单例并且它是用
初始化的<prop key="hibernate.current_session_context_class">thread</prop>
没有事务启动,我的 dao 仍然返回相同的异常。
【问题讨论】:
-
这个
org.hibernate.HibernateException: No Session found for current thread异常告诉您您没有正确配置事务并且不能通过简单地使用另一个当前会话上下文来解决。您的VersionService不是 Spring 托管的 bean,因此此类事务将无法处理。确保它是一个完全 sprnig 托管的 bean(包括代理创建)。 -
感谢您的回复。我不明白如何检查它。 Axis 让人难以理解。我的意思是,userMyAppDAO 被正确注入,所以我认为我正在使用 Spring 管理的 bean。
-
我找到了这个链接stackoverflow.com/questions/8846586/…。它建议将注释驱动的设置移动到 servlet 上下文中,而不是将它们留在应用程序中。看来我应该创建一个axis2-servlet.xml并以某种方式将它传递给axis2 servlet。
标签: spring hibernate dependency-injection axis2 transactional