Skip to content

Commit

Permalink
Fixed @SharedConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrin committed Nov 26, 2017
1 parent 84611e3 commit de6eaaa
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@
<version>${spring.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions sniffy-test/sniffy-spring-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
<artifactId>spring-test</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -168,9 +169,9 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
public void afterTestMethod(TestContext testContext) throws Exception {

Object sharedConnectionDataSourcesAttribute = getAttribute(testContext, SHARED_CONNECTION_DATASOURCES_ATTRIBUTE_NAME);
if (null != sharedConnectionDataSourcesAttribute && sharedConnectionDataSourcesAttribute instanceof Set) {
Set<SharedConnectionDataSource> sharedConnectionDataSources =
(Set<SharedConnectionDataSource>) sharedConnectionDataSourcesAttribute;
if (null != sharedConnectionDataSourcesAttribute && sharedConnectionDataSourcesAttribute instanceof Collection) {
@SuppressWarnings("unchecked") Collection<SharedConnectionDataSource> sharedConnectionDataSources =
(Collection<SharedConnectionDataSource>) sharedConnectionDataSourcesAttribute;
for (SharedConnectionDataSource sharedConnectionDataSource : sharedConnectionDataSources) {
sharedConnectionDataSource.resetMasterConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.h2.jdbcx.JdbcDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -19,6 +21,17 @@ public DataSource originalDataSource() throws SQLException {
return jdbcDataSource;
}

@Bean
public JdbcTemplate jdbcTemplate() throws SQLException {
DataSource dataSource = originalDataSource();
return new JdbcTemplate(dataSource);
}

@Bean
public DataSourceTransactionManager dataSourceTransactionManager() throws SQLException {
return new DataSourceTransactionManager(originalDataSource());
}

@Bean(destroyMethod = "close")
public Connection keepAliveConnection(DataSource originalDataSource) throws SQLException {
Connection keepAliveConnection = originalDataSource.getConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.sniffy.test.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.sql.*;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DataSourceTestConfiguration.class, SharedConnectionRollbackIntegrationTest.class})
@TestExecutionListeners(value = SniffySpringTestListener.class, mergeMode = MERGE_WITH_DEFAULTS)
@EnableSharedConnection
@Transactional
@Rollback
public class SharedConnectionRollbackIntegrationTest {

@Autowired
private JdbcTemplate jdbcTemplate;

@Test
@SharedConnection
public void testSharedConnectionTwoRows() throws SQLException, ExecutionException, InterruptedException {

jdbcTemplate.batchUpdate(
"INSERT INTO PUBLIC.PROJECT (ID, NAME) VALUES (SEQ_PROJECT.NEXTVAL, ?)",
Arrays.asList(new Object[]{"foo"}, new Object[]{"bar"})
);

assertEquals(2, newSingleThreadExecutor().submit(
() -> jdbcTemplate.queryForObject("SELECT COUNT(*) FROM PUBLIC.PROJECT", Integer.class)
).get().intValue());

}

@Test
@SharedConnection
public void testSharedConnectionThreeRows() throws SQLException, ExecutionException, InterruptedException {

jdbcTemplate.batchUpdate(
"INSERT INTO PUBLIC.PROJECT (ID, NAME) VALUES (SEQ_PROJECT.NEXTVAL, ?)",
Arrays.asList(new Object[]{"foo"}, new Object[]{"bar"}, new Object[]{"baz"})
);

assertEquals(3, newSingleThreadExecutor().submit(
() -> jdbcTemplate.queryForObject("SELECT COUNT(*) FROM PUBLIC.PROJECT", Integer.class)
).get().intValue());

}

}

0 comments on commit de6eaaa

Please sign in to comment.