简体   繁体   English

Spring声明式事务管理

[英]Spring declarative transaction management

In spring configuration file, my company's last developer declared as 在spring配置文件中,我公司的最后一位开发人员声明为

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
        <ref bean="autoCreateHocDS"/>
    </property>
</bean>

but there were no any AOP or annotation like @Transaction written to define on what class its should be applied. 但是没有编写任何AOP或注释(如@Transaction)来定义应在哪个类上应用它。

My first execution class here is OCsAutoCreateHocJob and it's internally call Service and dao classes. 我在这里的第一个执行类是OCsAutoCreateHocJob,它在内部调用Service和dao类。

So here my doubt is on what level transaction management will applied in class chain OR it will not apply without defining transaction level OR transaction management will applied to all classes? 因此,我的疑问是事务管理将应用于类链的哪个级别,或者在未定义事务级别的情况下将不适用,或者事务管理将应用于所有类?

You normally annotate your Service layer with @Transaction because this is the layer where you do your business logic (calculations, data manipulations, etc) that requires multiple calls to your DAO layer. 通常,您使用@Transaction注释服务层,因为这是您执行业务逻辑(计算,数据操作等)的层,需要多次调用DAO层。 This way, you can do a bunch of database methods using a single transaction, wherein you can rollback all database actions made in case something wrong happens. 这样,您可以使用一个事务来处理一堆数据库方法,其中,您可以回滚所有发生的数据库操作,以防发生错误。

<!-- proxy-target-class is set to true to use transactional scope -->
    <tx:annotation-driven proxy-target-class="true" transaction-manager="tomcatTransactionManager" />

<!-- Transaction Manager -->
    <bean id="tomcatTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource" />
    </bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM