-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgPoolConfiguration.java
More file actions
60 lines (54 loc) · 2.12 KB
/
PgPoolConfiguration.java
File metadata and controls
60 lines (54 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package nl.wouterh.pgpool.spring.example;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.sql.DataSource;
import nl.wouterh.pgpool.PgPoolConfig;
import nl.wouterh.pgpool.PooledDatabase;
import nl.wouterh.pgpool.common.example.CommonTestContainers;
import nl.wouterh.pgpool.liquibase.LiquibaseDatabaseInitializer;
import nl.wouterh.pgpool.spring.PgPoolDataSource;
import nl.wouterh.pgpool.spring.PgPoolDataSourceInitializerOverride;
import nl.wouterh.pgpool.spring.hikari.HikariDataSourceFactory;
import nl.wouterh.pgpool.testcontainers.PostgreSQLContainerConnectionProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.testcontainers.containers.JdbcDatabaseContainer;
@Configuration
public class PgPoolConfiguration {
@Bean
ExecutorService executorService() {
return Executors.newCachedThreadPool();
}
@Bean
PgPoolConfig pgPoolConfig(
SpringBeanTableFiller tableFiller,
DataSource dataSource,
ExecutorService executorService
) throws SQLException {
// Optionally start container concurrently, without blocking the spring context initialization
Future<JdbcDatabaseContainer> postgresContainer = executorService.submit(() -> {
CommonTestContainers.postgres.start();
return CommonTestContainers.postgres;
});
return PgPoolConfig.builder()
.executor(executorService)
.connectionProvider(new PostgreSQLContainerConnectionProvider(postgresContainer))
.waitForDropOnShutdown(true)
.dropThreads(2)
.pooledDatabase(PooledDatabase.builder()
.name("db1")
.createThreads(2)
.spares(5)
.listener(new HikariDataSourceFactory())
.initializer(new LiquibaseDatabaseInitializer("db/changelog/changelog.xml"))
.initializer(new PgPoolDataSourceInitializerOverride(dataSource, tableFiller))
.build())
.build();
}
@Bean
DataSource dataSource() {
return new PgPoolDataSource("db1");
}
}