【问题标题】:How to perform Spring LDAP and JPA transactions如何执行 Spring LDAP 和 JPA 事务
【发布时间】:2012-04-09 14:41:01
【问题描述】:

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

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

我的临时解决方案是在 DAO 层使用 JPATransactionManager,在服务层使用 LDAPTransactionManager。并确保在服务层最后调用 JPA DAO。

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

【问题讨论】:

  • 我正在尝试在 Spring LDAP 和通过 JPA 访问的数据库之间执行分布式事务

标签: java spring jpa ldap


【解决方案1】:

spring-ldap-1.3.1 documentation 的第 6.3 点告诉使用 用于 'jdbc-and-ldap' tx 目的的 ContextSourceAndDataSourceTransactionManager。 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"/>

玩得开心!

附言我差点忘了

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())

    );
}
}

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 2018-03-10
    • 2013-04-10
    • 2011-08-06
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多