Skip to content

Instantly share code, notes, and snippets.

@Romeh

Romeh/DbConfig.java

Created Sep 3, 2018
Embed
What would you like to do?
/**
* the db spring configuration to use in production , to be replaced with actual production configuration , that is for local run only
*/
@Configuration
@EnableTransactionManagement
public class DbConfig {
private static final List<String> DEFAULT_ADDITIONAL_INIT_DB_PARAMS = Arrays
.asList("--nosync", "--locale=en_US.UTF-8");
/**
* @param config the PostgresConfig configuration which will be used to get the needed host, port..
* @return the created DB datasource
*/
@Bean
@DependsOn("postgresProcess")
public DataSource dataSource(PostgresConfig config) {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl(format("jdbc:postgresql://%s:%s/%s", config.net().host(), config.net().port(), config.storage().dbName()));
ds.setUsername(config.credentials().username());
ds.setPassword(config.credentials().password());
return ds;
}
/**
* @return PostgresConfig that contains embedded db configuration like user name , password
* @throws IOException
*/
@Bean
public PostgresConfig postgresConfig() throws IOException {
// make it readable from configuration source file or system , it is hard coded here for explanation purpose only
final PostgresConfig postgresConfig = new PostgresConfig(Version.V9_6_8,
new AbstractPostgresConfig.Net("localhost", Network.getFreeServerPort()),
new AbstractPostgresConfig.Storage("test"),
new AbstractPostgresConfig.Timeout(),
new AbstractPostgresConfig.Credentials("user", "pass")
);
postgresConfig.getAdditionalInitDbParams().addAll(DEFAULT_ADDITIONAL_INIT_DB_PARAMS);
return postgresConfig;
}
/**
* @param config the PostgresConfig configuration to use to start Postgres db process
* @return PostgresProcess , the started db process
* @throws IOException
*/
@Bean(destroyMethod = "stop")
public PostgresProcess postgresProcess(PostgresConfig config) throws IOException {
PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
PostgresExecutable exec = runtime.prepare(config);
PostgresProcess process = exec.start();
return process;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.