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();
}
}
@vojtapol

This comment has been minimized.

Copy link

@vojtapol vojtapol commented Mar 15, 2018

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

This comment has been minimized.

Copy link
Owner Author

@jeffsheets jeffsheets commented May 30, 2018

@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

This comment has been minimized.

Copy link

@mrbusche 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

This comment has been minimized.

Copy link
Owner Author

@jeffsheets jeffsheets commented Dec 28, 2020

nice catch, thanks @mrbusche!

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