简体   繁体   中英

Spring declarative transaction management

In spring configuration file, my company's last developer declared as

 <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.

My first execution class here is OCsAutoCreateHocJob and it's internally call Service and dao classes.

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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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