Ldap遇到了事务管理问题

JDBC 和 LDAP 的事务管理

参考:http://www.linuxidc.com/Linux/2016-10/135718.htm

 

事务在项目中是必须考虑的一部分,这节讨论两种事务的分别处理和结合,通过注解来完成。 典型的JDBC事务配置如下:

<bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" /></bean>
 <tx:annotation-driven transaction-manager="txManager" />

我们配置了jdbc的默认事务管理为txManager,在服务层我们可以使用注解@Transcational来标注事务。

在单独需要ldap事务管理时,我们可以配置ldap的事务,起了个别名ldapTx:

<bean id="ldapTxManager"
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager">
    <property name="contextSource" ref="contextSource" /><qualifier value="ldapTx"/></bean>

我们可以使用注解@Transactional("ldapTx")来标注ldap的事务,如果不想每次引用别名,使用@LdapTransactional,则可以创建注解:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.transaction.annotation.Transactional;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Transactional("ldapTx")
public @interface LdapTransactional {

}

在ldap和jdbc同时都有操作的服务中,我们可以配置ContextSourceAndDataSourceTransactionManager来实现事务管理:

<beanid="ldapJdbcTxManager"class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager">  
    <property name="contextSource" ref="contextSource"/>  
    <property name="dataSource" ref="dataSource" />  
    <qualifiervalue="ldapJdbcTx"/>
</bean>
posted @ 2017-11-25 13:12  Actexpler  阅读(503)  评论(0编辑  收藏  举报