Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embedded MariaDB4j Spring Boot Configuration
#Setting to blank will put db into a tmp directory and recreate every test run
mariaDB4j.dataDir=
#Pick a random open port
mariaDB4j.port=0
#Location of db files. delete this directory if you need to recreate from scratch
mariaDB4j.dataDir=./data/local
#Default is 3306, so using 3307 just in case it is already running on this machine
mariaDB4j.port=3307
app.mariaDB4j.databaseName=app_alpha
spring.datasource.url=jdbc:mariadb://localhost:3307/
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
import ch.vorburger.mariadb4j.DBConfigurationBuilder
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import javax.sql.DataSource
@Configuration
@Profile(['local', 'integrationTest'])
class EmbeddedMariaDbConfig {
@Bean
MariaDB4jSpringService mariaDB4jSpringService() {
new MariaDB4jSpringService()
}
@Bean
DataSource dataSource(MariaDB4jSpringService mariaDB4jSpringService,
@Value('${app.mariaDB4j.databaseName}') String databaseName,
@Value('${spring.datasource.username}') String datasourceUsername,
@Value('${spring.datasource.password}') String datasourcePassword,
@Value('${spring.datasource.driver-class-name}') String datasourceDriver) {
//Create our database with default root user and no password
mariaDB4jSpringService.getDB().createDB(databaseName)
DBConfigurationBuilder config = mariaDB4jSpringService.getConfiguration()
DataSourceBuilder
.create()
.username(datasourceUsername)
.password(datasourcePassword)
.url(config.getURL(databaseName))
.driverClassName(datasourceDriver)
.build();
}
}
@itDarashukD
Copy link

itDarashukD commented Apr 23, 2021

This worked code:
@configuration
@Profile({"test"})
class EmbeddedMariaDBConfig {

@Bean
MariaDB4jSpringService mariaDB4jSpringService() throws ManagedProcessException {
    return new MariaDB4jSpringService();
}

@Bean
DataSource dataSource(MariaDB4jSpringService mariaDB4jSpringService,
                      @Value("${app.mariaDB4j.databaseName}") String databaseName,
                      @Value("${spring.datasource.username}") String datasourceUsername,
                      @Value("${spring.datasource.password}") String datasourcePassword,
                      @Value("${spring.datasource.driver-class-name}") String datasourceDriver) throws ManagedProcessException {
    //Create our database with default root user and no password
    mariaDB4jSpringService.getDB().createDB(databaseName);
    mariaDB4jSpringService.getDB().source("music_player_tables.sql");
    mariaDB4jSpringService.getDB().source("insert_data_into_album.sql");

    DBConfigurationBuilder config = mariaDB4jSpringService.getConfiguration();

    return DataSourceBuilder
            .create()
            .username(datasourceUsername)
            .password(datasourcePassword)
            .url(config.getURL(databaseName))
            .driverClassName(datasourceDriver)
            .build();
}

}


application-test.properties

spring.jpa.show-sql=true
server.error.include-message=always

app.mariaDB4j.databaseName=music_player
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

mariaDB4j.dataDir=
#Pick a random open port
mariaDB4j.port=0

@lovewithmind
Copy link

lovewithmind commented Apr 26, 2021

What version of vorburger mariaDB4j need to be used . I am trying the same but its not working for me . Version of mariadb4j used by me is 2.2.3.

Looking for some help !!
@itDarashukD

@itDarashukD
Copy link

itDarashukD commented Apr 26, 2021

This my dependency::

org.mariadb.jdbc mariadb-java-client 2.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment