本是甚是喜欢JPA简单方便明了,所以在LDAP项目要加上MYSQL数据库,但LDAP没有与jpa连用的事物处理 是一个很头疼的事,找到源码 发现里面只有2种对组合事物的支持:
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