Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Last active April 26, 2021 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsheets/780819e09654077db4fe4087f378e78d to your computer and use it in GitHub Desktop.
Save jeffsheets/780819e09654077db4fe4087f378e78d to your computer and use it in GitHub Desktop.
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();
}
}
@vojtapol
Copy link

The dataSource bean method is missing a return.

I would also like to see an example of how automatically wiping the MariaDB4 storage after test to mimic H2.

@jeffsheets
Copy link
Author

@vojtapol, since this is groovy, the last line is a returned without having the actual word return.

And as long as mariaDB4j.dataDir= is left blank this code will recreate the storage everytime like H2.

@mrbusche
Copy link

mrbusche commented Dec 28, 2020

FYI to anyone coming here and receiving an error of The used command is not allowed with this MariaDB version even though you've updated spring.datasource.url to include ?allowLoadLocalInfile=true. That value doesn't persist on line 35 and you'll need to pass that in explicitly.
.url(config.getURL(databaseName) + "?allowLoadLocalInfile=true")

@jeffsheets
Copy link
Author

nice catch, thanks @mrbusche!

@itDarashukD
Copy link

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

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