Skip to content

TransactionalTestExecutionListener uses cached TransactionManager (TM), even after ApplicationContext that created the TM has been marked 'dirty' and closed. [SPR-4638] #9315

Closed

Description

@spring-projects-issues

Jason Zaugg opened SPR-4638 and commented

TransactionalTestExecutionListener:378

	protected final PlatformTransactionManager getTransactionManager(final TestContext testContext) throws Exception {

		if (this.transactionManager != null) {
			return this.transactionManager;
		}

		// else...

		final TransactionConfigurationAttributes configAttributes = retrieveTransactionConfigurationAttributes(testContext.getTestClass());
		final String transactionManagerName = configAttributes.getTransactionManagerName();

		try {
			this.transactionManager = (PlatformTransactionManager) testContext.getApplicationContext().getBean(
					transactionManagerName, PlatformTransactionManager.class);
		}

I suggest that you do the following instead:

		if (this.transactionManager != null && this.applicationContext = testContext.getApplicationContext()) {
			return this.transactionManager;
		}

		// else...

		final TransactionConfigurationAttributes configAttributes = retrieveTransactionConfigurationAttributes(testContext.getTestClass());
		final String transactionManagerName = configAttributes.getTransactionManagerName();

		try {
			this.transactionManager = (PlatformTransactionManager) testContext.getApplicationContext().getBean(
					transactionManagerName, PlatformTransactionManager.class);
                        this.applicationContext = testContext.getApplicationContext();
		}

I experience this when I am using a custom TestRunner and TestContextManager, who are responsible for injecting Mock objects into the ApplicationWiring if the Test Case contains suitably annotated fields. As a workaround, my custom TestContextManager reflectively sets the TransactionalTestExecutionListener.transactionManager to null after each test.

public final class RaptorTestRunner extends SpringJUnit4ClassRunner {
    public RaptorTestRunner(Class testClass) throws InitializationError {
        super(testClass);
    }

    protected TestContextManager createTestContextManager(Class<?> clazz) {
        TestContextManager contextManager = new TestContextManager(clazz) {
            public TestInstancePreparer preparer;

            public void prepareTestInstance(Object testInstance) throws Exception {
                TestContext testContext = getTestContext();
                ApplicationContext springApplicationContext = testContext.getApplicationContext();
                Map map = springApplicationContext.getBeansOfType(Context.class);
                Context raptorContext = (Context) toArray(map.values().iterator())[0];
                preparer = new TestInstancePreparer(testInstance, testContext, raptorContext);
                preparer.prepareSubject();
            }

            public void afterTestMethod(Object testInstance, Method testMethod, Throwable exception) throws Exception {
                super.afterTestMethod(testInstance, testMethod, exception);
                preparer.discardApplicationContextIfInjectWasUsed();
                forceSpringTransactionTestListenerToUseTheLatestDatasource();
            }

            private void forceSpringTransactionTestListenerToUseTheLatestDatasource() throws NoSuchFieldException, IllegalAccessException {
                List<TestExecutionListener> listeners = getTestExecutionListeners();
                for (TestExecutionListener listener : listeners) {
                    // TODO Raise line 378 of TransactionalTestExecutionListener returns a transaction
                    //      manager that can point to a datasource that has been closed, if the spring application context
                    //      for the first test has been marked as dirty and closed.
                    if (listener instanceof TransactionalTestExecutionListener) {
                        Field field = listener.getClass().getDeclaredField("transactionManager");
                        field.setAccessible(true);
                        field.set(listener, null);
                    }
                }
            }


        };
        return contextManager;
    }
}

Affects: 2.5.1

Issue Links:

Activity

  1. spring-projects-issues commented on Mar 28, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Thanks for the suggestion! TransactionalTestExecutionListener is currently under heavy revision for other reasons already, so I've sneaked this in right away - although not as ApplicationContext comparison condition but rather as freshly obtaining the transaction manager for every transaction. We cache the parsed TransactionConfigurationAttributes now so the actual transaction manager getBean call is essentially just a hash lookup.

    Juergen

  2. spring-projects-issues commented on Jun 25, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Reopened in order to format code.

  3. added this to the 2.5.3 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions