ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何执行Spring LDAP和JPA事务

2019-12-01 15:19:41  阅读:349  来源: 互联网

标签:ldap jpa spring java


我更喜欢使用通过LocalContainerEntityManagerFactoryBean获得的JPA Entitymanager工厂进行持久化.
但是,当我想支持spring ldap和JPA之间的事务时,我碰壁了.我的服务层同时调用LDAP dao和JPA dao.
春天的ldap具有ContextSourceAndHibernateTransactionManager,它需要sessionFactory
而且它没有ContextSourceAndJPATransactionManager
当我将ContextSourceAndDataSourceTransactionManager与为LocalContainerEntityManagerFactoryBean配置的相同数据源一起使用时,它甚至不保留记录!

我不确定是否可以使用JTATransactionManager,因为LDAP不支持XA.

我的临时解决方案是在DAO层上使用JPATransactionManager并在服务层上使用LDAPTransactionManager.并确保在服务层中最后一个调用JPA DAO.

如果您愿意,我可以提供代码片段.
谢谢

解决方法:

spring-ldap-1.3.1 documentation的6.3点告诉使用
ContextSourceAndDataSourceTransactionManager,用于“ jdbc-ldap” tx目的. spring-ldap-core-1.3.1还具有ContextSourceAndHibernateTransactionManager.

展望这两个,一个人可以轻松创建自己的
ContextSourceAndJpaTransactionManager
并将其配置为

<bean id="jpaUnderLdapTransactionManager" class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndJpaTransactionManager">
    <property name="contextSource" ref="ldapSimpleContextSource"/>
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="jpaUnderLdapTransactionManager"/>

玩得开心!

ps.我差点忘了

public class ContextSourceAndJpaTransactionManager extends JpaTransactionManager
{

private static final long serialVersionUID = 1L;

private ContextSourceTransactionManagerDelegate ldapManagerDelegate =
        new ContextSourceTransactionManagerDelegate();

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#isExistingTransaction(Object)
 */
protected boolean isExistingTransaction(Object transaction)
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    return super.isExistingTransaction(actualTransactionObject
                                               .getJpaTransactionObject());
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doGetTransaction()
 */
protected Object doGetTransaction() throws TransactionException
{
    Object dataSourceTransactionObject = super.doGetTransaction();
    Object contextSourceTransactionObject =
            ldapManagerDelegate.doGetTransaction();

    return new ContextSourceAndJpaTransactionObject(
            contextSourceTransactionObject, dataSourceTransactionObject
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doBegin(java.lang.Object,
 *      org.springframework.transaction.TransactionDefinition)
 */
protected void doBegin(Object transaction, TransactionDefinition definition)
        throws TransactionException
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    super.doBegin(actualTransactionObject.getJpaTransactionObject(),
                  definition);
    ldapManagerDelegate.doBegin(
            actualTransactionObject.getLdapTransactionObject(),
            definition
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doCleanupAfterCompletion(java.lang.Object)
 */
protected void doCleanupAfterCompletion(Object transaction)
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) transaction;

    super.doCleanupAfterCompletion(actualTransactionObject
                                           .getJpaTransactionObject());
    ldapManagerDelegate.doCleanupAfterCompletion(actualTransactionObject
                                                 .getLdapTransactionObject());
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
 */
protected void doCommit(DefaultTransactionStatus status)
        throws TransactionException
{

    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) status.getTransaction();

    try
    {
        super.doCommit(new DefaultTransactionStatus(
                actualTransactionObject.getJpaTransactionObject(),
                status.isNewTransaction(),
                status.isNewSynchronization(),
                status.isReadOnly(),
                status.isDebug(),
                status.getSuspendedResources())
        );
    }
    catch (TransactionException ex)
    {
        if (isRollbackOnCommitFailure())
        {
            logger.debug("Failed to commit db resource, rethrowing", ex);
            // If we are to rollback on commit failure, just rethrow the
            // exception - this will cause a rollback to be performed on
            // both resources.
            throw ex;
        }
        else
        {
            logger.warn(
                    "Failed to commit and resource is rollbackOnCommit not set -"
                            + " proceeding to commit ldap resource.");
        }
    }
    ldapManagerDelegate.doCommit(new DefaultTransactionStatus(
            actualTransactionObject.getLdapTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doRollback(org.springframework.transaction.support.DefaultTransactionStatus)
 */
protected void doRollback(DefaultTransactionStatus status) throws TransactionException
{
    ContextSourceAndJpaTransactionObject actualTransactionObject =
            (ContextSourceAndJpaTransactionObject) status.getTransaction();

    super.doRollback(new DefaultTransactionStatus(
            actualTransactionObject.getJpaTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
    ldapManagerDelegate.doRollback(new DefaultTransactionStatus(
            actualTransactionObject.getLdapTransactionObject(),
            status.isNewTransaction(),
            status.isNewSynchronization(),
            status.isReadOnly(),
            status.isDebug(),
            status.getSuspendedResources())
    );
}

@SuppressWarnings("UnusedDeclaration")
public ContextSource getContextSource()
{
    return ldapManagerDelegate.getContextSource();
}

public void setContextSource(ContextSource contextSource)
{
    ldapManagerDelegate.setContextSource(contextSource);
}

@SuppressWarnings("UnusedDeclaration")
protected void setRenamingStrategy(TempEntryRenamingStrategy renamingStrategy)
{
    ldapManagerDelegate.setRenamingStrategy(renamingStrategy);
}

private final static class ContextSourceAndJpaTransactionObject
{
    private Object ldapTransactionObject;

    private Object jpaTransactionObject;

    public ContextSourceAndJpaTransactionObject(
            Object ldapTransactionObject, Object jpaTransactionObject)
    {
        this.ldapTransactionObject = ldapTransactionObject;
        this.jpaTransactionObject = jpaTransactionObject;
    }

    public Object getJpaTransactionObject()
    {
        return jpaTransactionObject;
    }

    public Object getLdapTransactionObject()
    {
        return ldapTransactionObject;
    }
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doSuspend(java.lang.Object)
 */
protected Object doSuspend(Object transaction) throws TransactionException
{
    throw new TransactionSuspensionNotSupportedException(
            "Transaction manager [" + getClass().getName()
                    + "] does not support transaction suspension");
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doResume(java.lang.Object, java.lang.Object)
 */
protected void doResume(Object transaction, Object suspendedResources)
        throws TransactionException
{
    throw new TransactionSuspensionNotSupportedException(
            "Transaction manager [" + getClass().getName()
                    + "] does not support transaction suspension");
}

/**
 * @see org.springframework.orm.jpa.JpaTransactionManager#doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus)
 */
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status)
{
    super.doSetRollbackOnly(
            new DefaultTransactionStatus(
                ((ContextSourceAndJpaTransactionObject)status.getTransaction())
                        .getJpaTransactionObject(),
                status.isNewTransaction(),
                status.isNewSynchronization(),
                status.isReadOnly(),
                status.isDebug(),
                status.getSuspendedResources())

    );
}
}

标签:ldap,jpa,spring,java
来源: https://codeday.me/bug/20191201/2081728.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有