JPA transactions used by LDAP and MySql

Source: Internet
Author: User
Tags ldap

I like JPA very much. It is simple and easy to understand. Therefore, MYSQL databases must be added to the LDAP project, but it is a headache that LDAP does not use with jpa for transaction processing, only two types of combined things are supported in the source code discovery:

Support the combination of ContextSourceAndDataSourceTransactionManager LDAP and JDBC

Support the combination of ContextSourceAndHibernateTransactionManager LDAP and Hibernate

If this is used, it would be uncomfortable to say that you still want to encapsulate jdbc. So I found a solution from a foreigner and wrote a method to complete the group of LDAP and JPA, the following is the code


import org.springframework.ldap.core.ContextSource;import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManagerDelegate;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionException;import org.springframework.transaction.TransactionSuspensionNotSupportedException;import org.springframework.transaction.support.DefaultTransactionStatus;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())    );}public ContextSource getContextSource(){    return ldapManagerDelegate.getContextSource();}public void setContextSource(ContextSource contextSource){    ldapManagerDelegate.setContextSource(contextSource);}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.isNewTransaction(),                status.isNewSynchronization(),                status.isReadOnly(),                status.isDebug(),                status.getSuspendedResources())    );}}



The man who wrote this story has never had time to translate and so on. He has time to study and write the annotations... Then your tx. xml will support it, so let's take a look at my tx. xml

<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: p = "http://www.springframework.org/schema/p" xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: jpa = "h Ttp: // www.springframework.org/schema/data/jpa "xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http :/ /Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "default-lazy-init =" true "default-autowire =" byName "> <bean id =" transactionManager "class = "Com. smarcloud. control. util. ContextSourceAndJpaTransactionManager"> <! -- The first one is LDAP --> <property name = "contextSource" ref = "contextSource"/> <! -- The second is Spring-Date-Jpa --> <property name = "entityManagerFactory" ref = "entityManagerFactory"/> </bean> <tx: annotation-driven transaction-manager = "transactionManager"/> </beans>


In this way, it is very easy to use. For an organization that deletes ldap, if there are groups below, please do not care about things, otherwise, you will find that you have not deleted it and an error is returned.

This article from the "Write down on the right" blog, please be sure to keep this source http://jueshizhanhun.blog.51cto.com/4372226/1259803

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.