From 3fffbf1408cf4be081e24b473383cbeadf405926 Mon Sep 17 00:00:00 2001
From: Mattias Arthursson
Date: Mon, 12 Feb 2007 17:28:24 +0000
Subject: [PATCH] Introduced ContextSourceTransactionManagerDelegate and
ContextSourceAndDataSourceTransactionManager.
---
sandbox/.classpath | 2 +
sandbox/ivy.xml | 211 ++++++++++--------
...rceTransactionManagerIntegrationTest.java} | 140 ++++++++++--
.../ldap/transaction/core/DummyDao.java | 9 +-
...Impl.java => LdapAndJdbcDummyDaoImpl.java} | 77 +++----
.../core/ContextSourceTransactionManager.java | 151 +++++--------
6 files changed, 335 insertions(+), 255 deletions(-)
rename sandbox/src/itest/java/org/springframework/ldap/transaction/core/{ContextSourceTransactionManagerIntegrationTest.java => ContextSourceAndDataSourceTransactionManagerIntegrationTest.java} (57%)
rename sandbox/src/itest/java/org/springframework/ldap/transaction/core/{DummyDaoImpl.java => LdapAndJdbcDummyDaoImpl.java} (54%)
diff --git a/sandbox/.classpath b/sandbox/.classpath
index d64123a4..b81fef06 100644
--- a/sandbox/.classpath
+++ b/sandbox/.classpath
@@ -49,5 +49,7 @@
+
+
diff --git a/sandbox/ivy.xml b/sandbox/ivy.xml
index 32ffab04..c3fdd096 100644
--- a/sandbox/ivy.xml
+++ b/sandbox/ivy.xml
@@ -1,105 +1,134 @@
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
diff --git a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManagerIntegrationTest.java b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java
similarity index 57%
rename from sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManagerIntegrationTest.java
rename to sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java
index d03d2a15..11281cad 100644
--- a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManagerIntegrationTest.java
+++ b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java
@@ -1,23 +1,35 @@
package org.springframework.ldap.transaction.core;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
import org.springframework.ldap.LdapServerManager;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
-public class ContextSourceTransactionManagerIntegrationTest extends
+/**
+ * Integration tests for {@link ContextSourceAndDataSourceTransactionManager}.
+ *
+ * @author Mattias Arthursson
+ */
+public class ContextSourceAndDataSourceTransactionManagerIntegrationTest extends
AbstractDependencyInjectionSpringContextTests {
private static Log log = LogFactory
- .getLog(ContextSourceTransactionManagerIntegrationTest.class);
+ .getLog(ContextSourceAndDataSourceTransactionManagerIntegrationTest.class);
- public ContextSourceTransactionManagerIntegrationTest() {
+ public ContextSourceAndDataSourceTransactionManagerIntegrationTest() {
setAutowireMode(AbstractDependencyInjectionSpringContextTests.AUTOWIRE_BY_NAME);
}
@@ -25,6 +37,8 @@ public class ContextSourceTransactionManagerIntegrationTest extends
private LdapTemplate ldapTemplate;
+ private JdbcTemplate jdbcTemplate;
+
private LdapServerManager ldapServerManager;
public void setLdapServerManager(LdapServerManager ldapServerManager) {
@@ -44,7 +58,19 @@ public class ContextSourceTransactionManagerIntegrationTest extends
}
protected void onSetUp() throws Exception {
+ if (TransactionSynchronizationManager.isSynchronizationActive()) {
+ TransactionSynchronizationManager.clearSynchronization();
+ }
+
ldapServerManager.cleanAndSetup("setup_data.ldif");
+ jdbcTemplate
+ .execute("create table PERSON(fullname VARCHAR, lastname VARCHAR, description VARCHAR)");
+ jdbcTemplate.update("insert into PERSON values(?, ?, ?)", new Object[] {
+ "Some Person", "Person", "Sweden, Company1, Some Person" });
+ }
+
+ protected void onTearDown() throws Exception {
+ jdbcTemplate.execute("drop table PERSON");
}
public void testCreateWithException() {
@@ -58,12 +84,27 @@ public class ContextSourceTransactionManagerIntegrationTest extends
log.debug("Verifying result");
+ // Verify that no entry was created
try {
ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden");
fail("NameNotFoundException expected");
} catch (NameNotFoundException expected) {
assertTrue(true);
}
+
+ try {
+ jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname='some testperson'",
+ new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ return null;
+ }
+ });
+ fail("EmptyResultDataAccessException expected");
+ } catch (EmptyResultDataAccessException expected) {
+ assertTrue(true);
+ }
}
public void testCreate() {
@@ -71,13 +112,24 @@ public class ContextSourceTransactionManagerIntegrationTest extends
"some description");
log.debug("Verifying result");
- ldapTemplate.lookup("cn=some testperson, ou=company1, c=Sweden");
+ Object ldapResult = ldapTemplate
+ .lookup("cn=some testperson, ou=company1, c=Sweden");
+ Object dbResult = jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname='some testperson'",
+ new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ return new Object();
+ }
+ });
+ assertNotNull(ldapResult);
+ assertNotNull(dbResult);
}
public void testUpdateWithException() {
String dn = "cn=Some Person,ou=company1,c=Sweden";
try {
- dummyDao.updateWithException(dn, "Updated Person",
+ dummyDao.updateWithException(dn, "Some Person", "Updated Person",
"Updated description");
fail("DummyException expected");
} catch (DummyException expected) {
@@ -86,7 +138,7 @@ public class ContextSourceTransactionManagerIntegrationTest extends
log.debug("Verifying result");
- Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
+ Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
public Object mapFromAttributes(Attributes attributes)
throws NamingException {
assertEquals("Person", attributes.get("sn").get());
@@ -96,15 +148,29 @@ public class ContextSourceTransactionManagerIntegrationTest extends
}
});
- assertNotNull(result);
+ Object jdbcResult = jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname=?",
+ new Object[] { "Some Person" }, new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ assertEquals("Person", rs.getString("lastname"));
+ assertEquals("Sweden, Company1, Some Person", rs
+ .getString("description"));
+ return new Object();
+ }
+ });
+
+ assertNotNull(ldapResult);
+ assertNotNull(jdbcResult);
}
public void testUpdate() {
String dn = "cn=Some Person,ou=company1,c=Sweden";
- dummyDao.update(dn, "Updated Person", "Updated description");
+ dummyDao.update(dn, "Some Person", "Updated Person",
+ "Updated description");
log.debug("Verifying result");
- Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
+ Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
public Object mapFromAttributes(Attributes attributes)
throws NamingException {
assertEquals("Updated Person", attributes.get("sn").get());
@@ -114,7 +180,20 @@ public class ContextSourceTransactionManagerIntegrationTest extends
}
});
- assertNotNull(result);
+ Object jdbcResult = jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname=?",
+ new Object[] { "Some Person" }, new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ assertEquals("Updated Person", rs.getString("lastname"));
+ assertEquals("Updated description", rs
+ .getString("description"));
+ return new Object();
+ }
+ });
+
+ assertNotNull(ldapResult);
+ assertNotNull(jdbcResult);
}
public void testUpdateAndRenameWithException() {
@@ -217,31 +296,39 @@ public class ContextSourceTransactionManagerIntegrationTest extends
String dn = "cn=Some Person,ou=company1,c=Sweden";
try {
// Perform test
- dummyDao.unbindWithException(dn);
+ dummyDao.unbindWithException(dn, "Some Person");
fail("DummyException expected");
} catch (DummyException expected) {
assertTrue(true);
}
// Verify result - check that the operation was properly rolled back
- Object result = ldapTemplate.lookup(dn, new AttributesMapper() {
+ Object ldapResult = ldapTemplate.lookup(dn, new AttributesMapper() {
public Object mapFromAttributes(Attributes attributes)
throws NamingException {
- assertEquals("Some Person", attributes.get("cn").get());
- assertEquals("Person", attributes.get("sn").get());
- assertEquals("Sweden, Company1, Some Person", attributes.get(
- "description").get());
+ // Just verify that the entry still exists.
return new Object();
}
});
- assertNotNull(result);
+ Object jdbcResult = jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname=?",
+ new Object[] { "Some Person" }, new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ // Just verify that the entry still exists.
+ return new Object();
+ }
+ });
+
+ assertNotNull(ldapResult);
+ assertNotNull(jdbcResult);
}
public void testUnbind() {
String dn = "cn=Some Person,ou=company1,c=Sweden";
// Perform test
- dummyDao.unbind(dn);
+ dummyDao.unbind(dn, "Some Person");
try {
// Verify result - check that the operation was not rolled back
@@ -251,5 +338,22 @@ public class ContextSourceTransactionManagerIntegrationTest extends
assertTrue(true);
}
+ try {
+ jdbcTemplate.queryForObject(
+ "select * from PERSON where fullname=?",
+ new Object[] { "Some Person" }, new RowMapper() {
+ public Object mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ return null;
+ }
+ });
+ fail("EmptyResultDataAccessException expected");
+ } catch (EmptyResultDataAccessException expected) {
+ assertTrue(true);
+ }
+ }
+
+ public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
+ this.jdbcTemplate = jdbcTemplate;
}
}
diff --git a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDao.java b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDao.java
index 33865442..77e8e274 100644
--- a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDao.java
+++ b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDao.java
@@ -7,9 +7,10 @@ public interface DummyDao {
void create(String country, String company, String fullname,
String lastname, String description);
- void update(String dn, String lastname, String description);
+ void update(String dn, String fullname, String lastname, String description);
- void updateWithException(String dn, String lastname, String description);
+ void updateWithException(String dn, String fullname, String lastname,
+ String description);
void updateAndRename(String dn, String newDn, String description);
@@ -21,8 +22,8 @@ public interface DummyDao {
void modifyAttributesWithException(String dn, String lastName,
String description);
- void unbind(String dn);
+ void unbind(String dn, String fullname);
- void unbindWithException(String dn);
+ void unbindWithException(String dn, String fullname);
}
diff --git a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDaoImpl.java b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/LdapAndJdbcDummyDaoImpl.java
similarity index 54%
rename from sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDaoImpl.java
rename to sandbox/src/itest/java/org/springframework/ldap/transaction/core/LdapAndJdbcDummyDaoImpl.java
index f4e9b1fe..98f99e90 100644
--- a/sandbox/src/itest/java/org/springframework/ldap/transaction/core/DummyDaoImpl.java
+++ b/sandbox/src/itest/java/org/springframework/ldap/transaction/core/LdapAndJdbcDummyDaoImpl.java
@@ -5,7 +5,7 @@ import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
-public class DummyDaoImpl implements DummyDao{
+public class LdapAndJdbcDummyDaoImpl implements DummyDao {
private LdapTemplate ldapTemplate;
private JdbcTemplate jdbcTemplate;
@@ -21,13 +21,10 @@ public class DummyDaoImpl implements DummyDao{
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#createWithException(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#createWithException(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#createWithException(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
- */
public void createWithException(String country, String company,
String fullname, String lastname, String description) {
create(country, company, fullname, lastname, description);
@@ -37,13 +34,10 @@ public class DummyDaoImpl implements DummyDao{
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#create(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#create(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#create(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
- */
public void create(String country, String company, String fullname,
String lastname, String description) {
DistinguishedName dn = new DistinguishedName();
@@ -57,70 +51,63 @@ public class DummyDaoImpl implements DummyDao{
ctx.setAttributeValue("sn", lastname);
ctx.setAttributeValue("description", description);
ldapTemplate.bind(dn, ctx, null);
-// jdbcTemplate.execute("insert into test values(1, 'kalle', 'pettersson', 123)");
+ jdbcTemplate.update("insert into PERSON values(?, ?, ?)", new Object[] {
+ fullname, lastname, description });
}
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#update(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#update(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#update(java.lang.String, java.lang.String, java.lang.String)
- */
- public void update(String dn, String lastname, String description) {
+ public void update(String dn, String fullname, String lastname,
+ String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("sn", lastname);
ctx.setAttributeValue("description", description);
ctx.update();
ldapTemplate.rebind(dn, ctx, null);
+ jdbcTemplate
+ .update(
+ "update PERSON set lastname=?, description = ? where fullname = ?",
+ new Object[] { lastname, description, fullname });
}
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#updateWithException(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#updateWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#updateWithException(java.lang.String, java.lang.String, java.lang.String)
- */
- public void updateWithException(String dn, String lastname,
- String description) {
- update(dn, lastname, description);
+ public void updateWithException(String dn, String fullname,
+ String lastname, String description) {
+ update(dn, fullname, lastname, description);
throw new DummyException("This method failed.");
}
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#updateAndRename(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#updateAndRename(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#updateAndRename(java.lang.String, java.lang.String, java.lang.String)
- */
public void updateAndRename(String dn, String newDn, String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("description", description);
ctx.update();
ldapTemplate.rebind(dn, ctx, null);
-
ldapTemplate.rename(dn, newDn);
}
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#updateAndRenameWithException(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#updateAndRenameWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#updateAndRenameWithException(java.lang.String, java.lang.String, java.lang.String)
- */
public void updateAndRenameWithException(String dn, String newDn,
String description) {
updateAndRename(dn, newDn, description);
@@ -130,12 +117,9 @@ public class DummyDaoImpl implements DummyDao{
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#modifyAttributes(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributes(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#modifyAttributes(java.lang.String, java.lang.String, java.lang.String)
- */
public void modifyAttributes(String dn, String lastName, String description) {
DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
ctx.setAttributeValue("sn", lastName);
@@ -147,12 +131,9 @@ public class DummyDaoImpl implements DummyDao{
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#modifyAttributesWithException(java.lang.String,
+ * @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributesWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#modifyAttributesWithException(java.lang.String, java.lang.String, java.lang.String)
- */
public void modifyAttributesWithException(String dn, String lastName,
String description) {
modifyAttributes(dn, lastName, description);
@@ -162,25 +143,21 @@ public class DummyDaoImpl implements DummyDao{
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#unbind(java.lang.String)
+ * @see org.springframework.ldap.transaction.core.DummyDao#unbind(java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#unbind(java.lang.String)
- */
- public void unbind(String dn) {
+ public void unbind(String dn, String fullname) {
ldapTemplate.unbind(dn);
+ jdbcTemplate.update("delete from PERSON where fullname=?",
+ new Object[] { fullname });
}
/*
* (non-Javadoc)
*
- * @see org.springframework.ldap.support.transaction.DummyDao#unbindWithException(java.lang.String)
+ * @see org.springframework.ldap.transaction.core.DummyDao#unbindWithException(java.lang.String)
*/
- /* (non-Javadoc)
- * @see org.springframework.ldap.support.transaction.TempDummyDao#unbindWithException(java.lang.String)
- */
- public void unbindWithException(String dn) {
- unbind(dn);
+ public void unbindWithException(String dn, String fullname) {
+ unbind(dn, fullname);
throw new DummyException("This operation failed.");
}
}
diff --git a/sandbox/src/main/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManager.java b/sandbox/src/main/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManager.java
index 1450f8c9..1e179fab 100644
--- a/sandbox/src/main/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManager.java
+++ b/sandbox/src/main/java/org/springframework/ldap/transaction/core/ContextSourceTransactionManager.java
@@ -15,19 +15,14 @@
*/
package org.springframework.ldap.transaction.core;
-import javax.naming.NamingException;
-import javax.naming.directory.DirContext;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.CompensatingTransactionOperationExecutor;
import org.springframework.ldap.transaction.CompensatingTransactionOperationRecorder;
+import org.springframework.ldap.transaction.DefaultCompensatingTransactionOperationManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* TransactionManager for managing LDAP transactions. Since transactions are not
@@ -92,56 +87,27 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* {@link DefaultTempEntryRenamingStrategy}), specified in
* {@link #setRenamingStrategy(TempEntryRenamingStrategy)}.
*
+ *
+ * The actual work of this Transaction Manager is delegated to a
+ * {@link ContextSourceTransactionManagerDelegate}. This is because the exact
+ * same logic needs to be used if we want to wrap a JDBC and LDAP transaction in
+ * the same logical transaction.
+ *
*
* @author Mattias Arthursson
+ *
+ * @see ContextSourceAndDataSourceTransactionManager
+ * @see ContextSourceTransactionManagerDelegate
+ * @see DefaultCompensatingTransactionOperationManager
+ * @see TempEntryRenamingStrategy
+ * @see TransactionAwareContextSourceProxy
*/
public class ContextSourceTransactionManager extends
AbstractPlatformTransactionManager {
- private static final long serialVersionUID = -4308820955185446535L;
+ private static final long serialVersionUID = 7138208218687237856L;
- private static Log log = LogFactory
- .getLog(ContextSourceTransactionManager.class);
-
- private ContextSource contextSource;
-
- private TempEntryRenamingStrategy renamingStrategy = new DefaultTempEntryRenamingStrategy();
-
- /**
- * Set the ContextSource to work on. Even though the actual ContextSource
- * sent to the LdapTemplate instance should be a
- * {@link TransactionAwareContextSourceProxy}, the one sent to this method
- * should be the target of that proxy. If it is not, the target will be
- * extracted and used instead.
- *
- * @param contextSource
- * the ContextSource to work on.
- */
- public void setContextSource(ContextSource contextSource) {
- if (contextSource instanceof TransactionAwareContextSourceProxy) {
- TransactionAwareContextSourceProxy proxy = (TransactionAwareContextSourceProxy) contextSource;
- this.contextSource = proxy.getTarget();
- } else {
- this.contextSource = contextSource;
- }
- }
-
- public ContextSource getContextSource() {
- return contextSource;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#doGetTransaction()
- */
- protected Object doGetTransaction() throws TransactionException {
- DirContextHolder contextHolder = (DirContextHolder) TransactionSynchronizationManager
- .getResource(this.contextSource);
- ContextSourceTransactionObject txObject = new ContextSourceTransactionObject(
- contextHolder);
- return txObject;
- }
+ private ContextSourceTransactionManagerDelegate delegate = new ContextSourceTransactionManagerDelegate();
/*
* (non-Javadoc)
@@ -151,17 +117,16 @@ public class ContextSourceTransactionManager extends
*/
protected void doBegin(Object transaction, TransactionDefinition definition)
throws TransactionException {
- ContextSourceTransactionObject txObject = (ContextSourceTransactionObject) transaction;
+ delegate.doBegin(transaction, definition);
+ }
- if (txObject.getContextHolder() == null) {
- DirContext newCtx = getContextSource().getReadOnlyContext();
- DirContextHolder contextHolder = new DirContextHolder(newCtx,
- renamingStrategy);
-
- txObject.setContextHolder(contextHolder);
- TransactionSynchronizationManager.bindResource(getContextSource(),
- contextHolder);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#doCleanupAfterCompletion(java.lang.Object)
+ */
+ protected void doCleanupAfterCompletion(Object transaction) {
+ delegate.doCleanupAfterCompletion(transaction);
}
/*
@@ -171,10 +136,16 @@ public class ContextSourceTransactionManager extends
*/
protected void doCommit(DefaultTransactionStatus status)
throws TransactionException {
- ContextSourceTransactionObject txObject = (ContextSourceTransactionObject) status
- .getTransaction();
- txObject.getContextHolder().getTransactionDataManager().commit();
+ delegate.doCommit(status);
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#doGetTransaction()
+ */
+ protected Object doGetTransaction() throws TransactionException {
+ return delegate.doGetTransaction();
}
/*
@@ -184,42 +155,38 @@ public class ContextSourceTransactionManager extends
*/
protected void doRollback(DefaultTransactionStatus status)
throws TransactionException {
- ContextSourceTransactionObject txObject = (ContextSourceTransactionObject) status
- .getTransaction();
- txObject.getContextHolder().getTransactionDataManager().rollback();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#doCleanupAfterCompletion(java.lang.Object)
- */
- protected void doCleanupAfterCompletion(Object transaction) {
- log.debug("Cleaning stored ContextHolder");
- TransactionSynchronizationManager.unbindResource(contextSource);
-
- ContextSourceTransactionObject txObject = (ContextSourceTransactionObject) transaction;
- DirContext ctx = txObject.getContextHolder().getCtx();
-
- try {
- log.debug("Closing target context");
- ctx.close();
- } catch (NamingException e) {
- e.printStackTrace();
- }
-
- txObject.getContextHolder().clear();
+ delegate.doRollback(status);
}
/**
- * Set the {@link TempEntryRenamingStrategy} to be used when renaming
- * temporary entries in unbind and rebind operations. Default value is a
- * {@link DefaultTempEntryRenamingStrategy}.
+ * Get the ContextSource.
+ *
+ * @return the contextSource.
+ * @see ContextSourceTransactionManagerDelegate#getContextSource()
+ */
+ public ContextSource getContextSource() {
+ return delegate.getContextSource();
+ }
+
+ /**
+ * Set the ContextSource.
+ *
+ * @param contextSource
+ * the ContextSource.
+ * @see ContextSourceTransactionManagerDelegate#setContextSource(ContextSource)
+ */
+ public void setContextSource(ContextSource contextSource) {
+ delegate.setContextSource(contextSource);
+ }
+
+ /**
+ * Set the {@link TempEntryRenamingStrategy}.
*
* @param renamingStrategy
- * the {@link TempEntryRenamingStrategy} to use.
+ * the Renaming Strategy.
+ * @see ContextSourceTransactionManagerDelegate#setRenamingStrategy(TempEntryRenamingStrategy)
*/
public void setRenamingStrategy(TempEntryRenamingStrategy renamingStrategy) {
- this.renamingStrategy = renamingStrategy;
+ delegate.setRenamingStrategy(renamingStrategy);
}
}