如果没有使用事务注释的活动事务错误,则获取保存无效

时间:2013-10-23 17:31:18

标签: java spring hibernate transactions jersey

我在我的应用程序中使用spring + hibernate + jersey。我想使用事务,所以我在服务层使用了spring的@Transactional注释。这是我的hibernate.cfg.xml:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
    <property name="hibernate.connection.username">user</property>
  </session-factory>
</hibernate-configuration>

我没有在这里使用session_context,所以spring可以管理它。在我的applicationCOntext.xml中,我定义了transactionManager:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db"/>
    <property name="user" value="username"/>
</bean>

<context:annotation-config/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.hibernate.pojos</value>
        </list>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

所有匹配/ api / v1 / *的url映射到名为jersey的servlet,使用的servlet类是com.sun.jersey.spi.spring.container.servlet.SpringServlet,我将com.services作为包扫描到了@Path("/app") @Component public class testApi() { @Autowired private DAOImpl daoimpl; @POST @Path("/create") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Transactional(rollbackFor = {Exception.class}) public Map<String,Object> testHello(user u) { Map response = daoimpl.save(u); return response; } } 。在这个包中我有一个类:

daoimpl

sessionFactory.getCurrentSession()已自动连接sessionFactory并使用daoimpl.save(obj)方法获取会话。方法{{1}}只将其保存在db中。因为我已将testHello标记为事务性,所以我期望一个事务开始由spring管理,然后控制应该转到实际保存发生的daoimpl。但是如果没有活动交易,我得到保存是无效的。我在hibernate配置中看到很多帖子提到了session_context,因此,spring无法处理事务。但在我的情况下,我确保我不提供任何session_context。我错过了什么?我甚至尝试将@transactional添加到DAO中,因为在我的示例应用程序中,我只是为服务发出一个数据库调用。但这也没有用。

1 个答案:

答案 0 :(得分:-1)

可能是您通过hibernate.cfg.xml指定会话工厂,然后再在Spring中将其传递给事务管理器。

不确定是否诚实,但是我从未使用过hibenate.cfg.xml,然后为我工作(根据您的指定添加了事务配置)。这也为您提供了将连接参数指定为属性并允许您通过Spring Profiles或其他一些机制轻松交换数据库配置的优势。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
                                                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="packagesToScan">
                        <list>
                                <value>uk.co.xyz.domain</value>
                        </list> 
                </property>
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.show_sql">${hibernate.showsql}</prop>
                                <prop key="hibernate.format_sql">true</prop>
                                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                                <prop key="hibernate.hbm2ddl.auto">${hibernate.ddlauto}</prop> 
                                <prop key="hibernate.cache.use_second_level_cache">${hibernate.enablecache}</prop>
                                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                        </props>
                </property>
        </bean>

        <bean id="transactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                        <ref local="sessionFactory" />
                </property>
        </bean>

</beans>

另请参见此处实际建议无关紧要:

applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

相关问题