Java Code Examples for org.springframework.transaction.support.TransactionSynchronizationManager#clearSynchronization()

The following examples show how to use org.springframework.transaction.support.TransactionSynchronizationManager#clearSynchronization() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityManagerFactoryUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDoGetEntityManagerWithTx() throws Exception {
	try {
		EntityManagerFactory factory = mock(EntityManagerFactory.class);
		EntityManager manager = mock(EntityManager.class);

		TransactionSynchronizationManager.initSynchronization();
		given(factory.createEntityManager()).willReturn(manager);

		// no tx active
		assertSame(manager, EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null));
		assertSame(manager, ((EntityManagerHolder)TransactionSynchronizationManager.unbindResource(factory)).getEntityManager());
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}

	assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
}
 
Example 2
Source File: EntityManagerFactoryUtilsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoGetEntityManagerWithTx() throws Exception {
	try {
		EntityManagerFactory factory = mock(EntityManagerFactory.class);
		EntityManager manager = mock(EntityManager.class);

		TransactionSynchronizationManager.initSynchronization();
		given(factory.createEntityManager()).willReturn(manager);

		// no tx active
		assertSame(manager, EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null));
		assertSame(manager, ((EntityManagerHolder)TransactionSynchronizationManager.unbindResource(factory)).getEntityManager());
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}

	assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
}
 
Example 3
Source File: ContextSourceAndHibernateTransactionManagerNamespaceITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Before
public void prepareTest() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}

	OrgPerson person = new OrgPerson();
	person.setId(new Integer(1));
	person.setLastname("Person");
	person.setFullname("Some Person");
	person.setDescription("Sweden, Company1, Some Person");
	person.setCountry("Sweden");
	person.setCompany("Company1");
	// "Some Person", "Person", "Sweden, Company1, Some Person"
	// avoid the transaction manager we have configured, do it manually
	Session session = this.sessionFactory.openSession();
	Transaction tx = session.beginTransaction();
	session.saveOrUpdate(person);
	tx.commit();
	session.close();

}
 
Example 4
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testClobStringTypeWithFlushOnCommit() throws Exception {
	given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

	ClobStringType type = new ClobStringType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.CLOB, type.sqlTypes()[0]);
	assertEquals(String.class, type.returnedClass());
	assertTrue(type.equals("content", "content"));
	assertEquals("content", type.deepCopy("content"));
	assertFalse(type.isMutable());

	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, "content", 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setClobAsString(ps, 1, "content");
}
 
Example 5
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobSerializableTypeWithNull() throws Exception {
	given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(null);

	BlobSerializableType type = new BlobSerializableType(lobHandler, null);
	assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, null, 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
 
Example 6
Source File: CompensatingTransactionUtilsTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    dirContextMock = mock(DirContext.class);
    contextSourceMock = mock(ContextSource.class);
    operationManagerMock = mock(CompensatingTransactionOperationManager.class);

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronizationManager.clearSynchronization();
    }
}
 
Example 7
Source File: ContextSourceAndDataSourceTransactionManagerIntegrationTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}

	jdbcTemplate.execute("drop table PERSON if exists");
	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" });
}
 
Example 8
Source File: BatchMetricsImplTest.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Test
public void decrementBy1Transactional() throws Exception {
	// Given
	TransactionSynchronizationManager.initSynchronization();
	// When
	batchMetrics.decrement("counter.test", 1L);
	TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	// Then
	TransactionSynchronizationManager.clearSynchronization();
}
 
Example 9
Source File: BatchMetricsImplTest.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Test
public void incrementBy1Transactional() throws Exception {
	// Given
	TransactionSynchronizationManager.initSynchronization();
	// When
	batchMetrics.increment("counter.test", 1L);
	TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	// Then
	TransactionSynchronizationManager.clearSynchronization();
}
 
Example 10
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobSerializableType() throws Exception {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	oos.writeObject("content");
	oos.close();

	given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(new ByteArrayInputStream(baos.toByteArray()));

	BlobSerializableType type = new BlobSerializableType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.BLOB, type.sqlTypes()[0]);
	assertEquals(Serializable.class, type.returnedClass());
	assertTrue(type.isMutable());

	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, "content", 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray());
}
 
Example 11
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobByteArrayType() throws Exception {
	byte[] content = "content".getBytes();
	given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);

	BlobByteArrayType type = new BlobByteArrayType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.BLOB, type.sqlTypes()[0]);
	assertEquals(byte[].class, type.returnedClass());
	assertTrue(type.equals(new byte[] {(byte) 255}, new byte[] {(byte) 255}));
	assertTrue(Arrays.equals(new byte[] {(byte) 255}, (byte[]) type.deepCopy(new byte[] {(byte) 255})));
	assertTrue(type.isMutable());

	assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, content, 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setBlobAsBytes(ps, 1, content);
}
 
Example 12
Source File: ContextSourceAndDataSourceTransactionManagerNamespaceITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}

	jdbcTemplate.execute("drop table PERSON if exists");
	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" });
}
 
Example 13
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobStringType() throws Exception {
	String content = "content";
	byte[] contentBytes = content.getBytes();
	given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes);

	BlobStringType type = new BlobStringType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.BLOB, type.sqlTypes()[0]);
	assertEquals(String.class, type.returnedClass());
	assertTrue(type.equals("content", "content"));
	assertEquals("content", type.deepCopy("content"));
	assertFalse(type.isMutable());

	assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, content, 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes);
}
 
Example 14
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testClobStringTypeWithSynchronizedSession() throws Exception {
	SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);
	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);
	given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

	ClobStringType type = new ClobStringType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.CLOB, type.sqlTypes()[0]);
	assertEquals(String.class, type.returnedClass());
	assertTrue(type.equals("content", "content"));
	assertEquals("content", type.deepCopy("content"));
	assertFalse(type.isMutable());

	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		SessionFactoryUtils.getSession(sf, true);
		type.nullSafeSet(ps, "content", 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(2, synchs.size());
		assertTrue(synchs.get(0).getClass().getName().endsWith("SpringLobCreatorSynchronization"));
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
		((TransactionSynchronization) synchs.get(1)).beforeCompletion();
		((TransactionSynchronization) synchs.get(1)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}

	verify(session).close();
	verify(lobCreator).setClobAsString(ps, 1, "content");
}
 
Example 15
Source File: LobTypeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testClobStringType() throws Exception {
	given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

	ClobStringType type = new ClobStringType(lobHandler, null);
	assertEquals(1, type.sqlTypes().length);
	assertEquals(Types.CLOB, type.sqlTypes()[0]);
	assertEquals(String.class, type.returnedClass());
	assertTrue(type.equals("content", "content"));
	assertEquals("content", type.deepCopy("content"));
	assertFalse(type.isMutable());

	assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
	TransactionSynchronizationManager.initSynchronization();
	try {
		type.nullSafeSet(ps, "content", 1);
		List synchs = TransactionSynchronizationManager.getSynchronizations();
		assertEquals(1, synchs.size());
		assertTrue(synchs.get(0).getClass().getName().endsWith("SpringLobCreatorSynchronization"));
		((TransactionSynchronization) synchs.get(0)).beforeCompletion();
		((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
	}
	finally {
		TransactionSynchronizationManager.clearSynchronization();
	}
	verify(lobCreator).setClobAsString(ps, 1, "content");
}
 
Example 16
Source File: ContextSourceTransactionManagerSubtreeIntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}
}
 
Example 17
Source File: ContextSourceTransactionManagerIntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}
}
 
Example 18
Source File: ContextSourceAndDataSourceTransactionManagerLdap179IntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronizationManager.clearSynchronization();
    }
}
 
Example 19
Source File: ContextSourceTransactionManagerNamespaceIntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}
}
 
Example 20
Source File: ContextSourceAndHibernateTransactionManagerLdap179IntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Before
public void prepareTest() throws Exception {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.clearSynchronization();
	}
}