LDAP 与 MySql 连用 的JPA事物问题
2013-07-29 17:05
155 查看
本事甚是喜欢JPA简单方便明了,所以在LDAP项目要加上MYSQL数据库,但LDAP没有与jpa连用的事物处理 是一个很头疼的事,找到源码 发现里面只有2种对组合事物的支持:
ContextSourceAndDataSourceTransactionManager LDAP与JDBC的组合支持
ContextSourceAndHibernateTransactionManager LDAP与Hibernate的组合支持
如果使用这个 就是要说自己还要去封装jdbc 非常不爽,所以找到了个老外给的解决方法,自己写一个方法,来完成LDAP与JPA的组事物,下面就是 那段代码
这事那人写的 一直没时间翻译 等有时间 得好好研究下 并写好注解。。。之后你的tx.xml要支持它 所以再看下我的tx.xml
这样就可以了 ,非常好用的 ,对了 对于删除ldap的一个组织时,下面如果有分组,请不要家事物,否者你会发现 你并没删除掉并且还报错。
本文出自 “记下就对了” 博客,请务必保留此出处http://jueshizhanhun.blog.51cto.com/4372226/1259803
ContextSourceAndDataSourceTransactionManager LDAP与JDBC的组合支持
ContextSourceAndHibernateTransactionManager LDAP与Hibernate的组合支持
如果使用这个 就是要说自己还要去封装jdbc 非常不爽,所以找到了个老外给的解决方法,自己写一个方法,来完成LDAP与JPA的组事物,下面就是 那段代码
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) */ @Override protected void doSetRollbackOnly(DefaultTransactionStatus status) { super.doSetRollbackOnly( new DefaultTransactionStatus( ((ContextSourceAndJpaTransactionObject)status.getTransaction()) .getJpaTransactionObject(), status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources()) ); } }
这事那人写的 一直没时间翻译 等有时间 得好好研究下 并写好注解。。。之后你的tx.xml要支持它 所以再看下我的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="http://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"> <!-- 第一个是LDAP的 --> <property name="contextSource" ref="contextSource"/> <!-- 第二个是Spring-Date-Jpa的 --> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
这样就可以了 ,非常好用的 ,对了 对于删除ldap的一个组织时,下面如果有分组,请不要家事物,否者你会发现 你并没删除掉并且还报错。
本文出自 “记下就对了” 博客,请务必保留此出处http://jueshizhanhun.blog.51cto.com/4372226/1259803
相关文章推荐
- 关于MySQL事物处理的问题
- java 使用mysql事物处理问题
- mysql 基本操作 触发器、锁、事物、分布式事物、sql安全问题、sql mode详解 (第五章)
- mysql的engine不同,导致事物回滚失败的问题
- spring boot 1.5.4使用JPA,遇到Mysql区分大小写的问题
- JPA中MySQLSyntaxErrorException问题解决方案
- jpa 操作mysql 别名问题
- ubuntu 安装mysql-python和 python-ldap,navicate 问题
- Python捕捉MySQL的警告导致的事物锁等待超时问题
- Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题
- spring boot JPA mySql 启动报错的问题
- MYSQL DDL语句 与 DML 在一个事物中的问题。
- mysql事物级别的问题。
- MySql的锁问题和事物
- Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题
- [记录]JPA/Hibernate的MySQL长连接问题
- mysql表名大写,造成jpa Table doesn't exist问题
- mysql中jpa开发针对BLOB字段的update问题
- Spring-boot+JPA+Hibernate项目修改数据库MySQL为Oracle出现的问题
- mysql事物 并发问题,锁机制