Skip to content

Use InheritableThreadLocal instead of ThreadLocal in TransactionSynchronizationManager [SPR-1514] #6213

@spring-projects-issues

Description

@spring-projects-issues

Jeff Badorek opened SPR-1514 and commented

It would be nice if a bound resource in TransactionSynchronizationManager was visible to child threads. I think this could be accomplished using InheritableThreadLocal.

Here is a test case that recreates the problem. (Changing to InheritableThreadLocal works.)

public void testTransactionSynchronizationManager() throws InterruptedException {
	final ArrayList actual = new ArrayList();
	final ArrayList expected = new ArrayList();
	final Object key = new Object();
	
	TransactionSynchronizationManager.bindResource(key, "value");
	
	actual.add(TransactionSynchronizationManager.getResource(key));
	
	Thread thread = new Thread() {
		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		public void run() {
			expected.add(TransactionSynchronizationManager.getResource(key));
		}
	};
	
	thread.start();
	
	thread.join();
	
	assertEquals(actual, expected);
}

Affects: 1.2.6

Referenced from: commits spring-projects/spring-batch@d203f2a

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions