春季启动@Transactional [英] Spring boot @Transactional

查看:50
本文介绍了春季启动@Transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

spring boot 是否会在控制器层自动添加@Transactional 注解?我尝试将 @Transactional 放在服务层,但似乎控制器层覆盖了注释.

Does spring boot automatically add @Transactional annotation at controller layer? I tried putting @Transactional at service layer but it seems that the controller layer is overriding the annotation.

我有这个配置

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find*" read-only="true" isolation="READ_COMMITTED"
            propagation="NOT_SUPPORTED" />
        <tx:method name="load*" read-only="true" isolation="READ_COMMITTED"
            propagation="NOT_SUPPORTED" />
        <tx:method name="get*" read-only="true" isolation="READ_COMMITTED"
            propagation="NOT_SUPPORTED" />
        <tx:method name="*" timeout="30" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config proxy-target-class="true">
    <aop:advisor advice-ref="txAdvice"
        pointcut="execution(* *..service.*Service*.*(..))" order="1" />
</aop:config>

即使我删除了该配置,交易仍然有效.

and even If I remove that config the transaction still works.

这是我的数据源配置

<bean id="msDataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.main.driverClass}" />
    <property name="url" value="${jdbc.main.url}" />
    <property name="username" value="${jdbc.main.username}" />
    <property name="password" value="${jdbc.main.password}" />

</bean>


<bean id="msPUM"
    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="defaultDataSource" ref="msDataSource" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="msPUM" />
    <!--<property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
        <property name="database" value="ORACLE"/> <property name="generateDdl" value="false"/> 
        <property name="showSql" value="true" /> </bean> </property> -->
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

推荐答案

我只是猜测,但我假设您在这里要问的是为什么您能够在控制器内的实体上加载惰性集合?

I'm just guessing but I assume what you're trying to ask here is why you're able to load lazy collections on your entities inside your controller?

Spring Boot 将以下应用程序属性 spring.jpa.open-in-view 配置为默认值true".基本上,这会为整个请求打开一个会话,允许您在 @Transactional 之外执行上述操作.

Spring Boot configures the following application property spring.jpa.open-in-view with a default value of "true". Basically this opens a session for the entire request allowing you to do things like the above outside of a @Transactional.

将此添加到您的 application.properties 将关闭它:

Adding this to your application.properties will turn it off:

spring.jpa.open-in-view=false

spring.jpa.open-in-view=false

这篇关于春季启动@Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆