Created
September 3, 2018 13:02
-
-
Save Romeh/826e1cf28f1ceeb720c611a41b978056 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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