【问题标题】:@Transactional not working in my Spring Tool Suit@Transactional 在我的 Spring Tool Suite 中不起作用
【发布时间】:2018-04-01 06:56:13
【问题描述】:

我在 Spring Tool Suite 中的项目存在问题。 @Transactional 注释不起作用。

这是我的项目结构:-

web.xml:-

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml:-

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.project.dateandcrud" />

root-context.xml:-

<!-- Root Context: defines shared resources visible to all other web components -->
<context:property-placeholder location="classpath:datasource-cfg.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${ds.driverClassName}" />
    <property name="url" value="${ds.url}" />
    <property name="username" value="${ds.username}" />
    <property name="password" value="${ds.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.project.dateandcrud.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
        </props>
    </property>
</bean> 

<!-- Transaction Manager to make Transaction Support-->
<tx:annotation-driven transaction-manager="transactionManager"/>

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

我使用 DAOIMPL 如下:-

@Repository
public class ProfileDaoImpl implements ProfileDao {

@Autowired
private SessionFactory sessionFactory;

private Session session;

@Override
@Transactional
public void addProfile(Profile p) {

    session = sessionFactory.openSession();
//      session.beginTransaction();
    session.save(p);
//      session.getTransaction().commit();
    session.close();

}

当我不使用@Transactional 注释时,我的项目工作正常。但是当使用注释时它不起作用。

我的 servlet-context.xml 或 root-context.xml 文件中是否有任何问题。

我试过移动 &lt;context:component-scan base-package="com.project.dateandcrud" /&gt; this 从 selvlet-context.xmlroot-context.xml 但这也不起作用。

请告诉我可能的错误。

【问题讨论】:

  • 不起作用是什么意思?有没有异常弹出?
  • @MaciejKowalski 没有错误弹出窗口,只是它没有将数据插入数据库......

标签: xml spring hibernate spring-mvc


【解决方案1】:

这是我找到的解决方案....只需将 &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;root-context.xml 移动到 servlet-context.xml...

【讨论】:

    【解决方案2】:

    不要试图自己打开/关闭休眠session,让spring-tx处理这个:

    @Transactional
    public void addProfile(Profile p) {
        final Session session = sessionFactory.getCurrentSession();
        session.save(p);
    }
    

    并将 &lt;annotation-driven/&gt;servlet-context.xml 移动到您的 root-context.xml 配置。

    这应该可以解决您的问题。

    【讨论】:

    • 但是当我这样做时会弹出以下错误:- 无法获得当前线程的事务同步会话
    • @Space 是的,你是对的。我只是仔细查看了您的配置并更新了我的答案。
    • 最终经过一些调整后,它起作用了...请查看我的答案...谢谢 Bodhdan
    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-07-30
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多