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

java-如何执行Spring LDAP和JPA事务,第1张

概述我更喜欢使用通过LocalContainerEntityManagerFactoryBean获得的JPA Entitymanager工厂进行持久化.但是,当我想支持spring ldap和JPA之间的事务时,我碰壁了.我的服务层同时调用LDAP dao和JPA dao.春天的ldap具有ContextSourceAndHibernateTransaction

我更喜欢使用通过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" >    <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.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.getSuspendedResources())    );    ldapManagerDelegate.doRollback(new DefaultTransactionStatus(            actualTransactionObject.getLdapTransactionObject(),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) */@OverrIDeprotected voID doSetRollbackOnly(DefaultTransactionStatus status){    super.doSetRollbackOnly(            new DefaultTransactionStatus(                ((ContextSourceAndJpaTransactionObject)status.getTransaction())                        .getJpaTransactionObject(),status.getSuspendedResources())    );}}
总结

以上是内存溢出为你收集整理的java-如何执行Spring LDAP和JPA事务 全部内容,希望文章能够帮你解决java-如何执行Spring LDAP和JPA事务 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1237104.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存