Skip to content

Commit b3549aa

Browse files
committed
STAND-139: Upgrade MariaDB4j to 3.3.1 and refactor ReusableDB to native API
1 parent 84d7192 commit b3549aa

File tree

9 files changed

+145
-211
lines changed

9 files changed

+145
-211
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,28 @@ SUMMARY: Using a single package for all (most) platforms approximately tripples
259259
MariaDB4j documentation can be found at:
260260
https://github.com/MariaDB4j/MariaDB4j
261261

262-
## 🛠️ Reusable Embedded MariaDB (ReusableDB.java)
262+
## 🛠️ Database Initialization Logic (MariaDB4j 3.3.1)
263263

264-
To improve startup robustness and cross-platform compatibility, especially on Windows, the OpenMRS Standalone project uses a custom wrapper around the `DB` class from MariaDB4j called `ReusableDB`.
264+
To improve startup robustness and cross-platform compatibility, especially on Windows, the OpenMRS Standalone project utilizes the native folder-reuse capabilities of **MariaDB4j 3.3.1**. This replaces the legacy custom `ReusableDB` wrapper.
265265

266266
#### 🔍 Purpose
267-
268-
`ReusableDB` avoids deleting the `dataDir` when the database is already initialized. This:
269-
- Prevents startup failures due to locked files on Windows.
270-
- Supports seamless restarts of the Standalone without losing data.
271-
- Makes switching between demo and empty databases more reliable.
267+
By leveraging modern MariaDB4j internal logic, the Standalone ensures:
268+
- **Data Persistence:** Existing data in the `dataDir` is automatically detected and preserved.
269+
- **Windows Robustness:** Avoids common file-locking issues on Windows by checking for existing data before attempting a fresh installation.
270+
- **Simplified Maintenance:** Leverages standard library calls instead of custom internal workarounds.
272271

273272
#### ✅ How it Works
274-
- Checks for the presence of the `openmrs` database directory inside `dataDir`.
275-
- If not found, triggers the initial MariaDB install process.
276-
- Otherwise, starts MariaDB using the existing configuration and data files.
273+
The `MariaDbController` now utilizes the improved `newEmbeddedDB` factory method:
274+
1. **Internal Check:** The library internally checks if the database directory already exists and contains data.
275+
2. **Smart Initialization:** * If the folder is **empty**, it proceeds with a fresh installation and schema setup.
276+
* If data is **present**, it skips the installation phase and safely opens the existing database.
277277

278-
#### 📦 Usage Example
278+
#### 📦 Usage Example (Standard API)
279279

280280
```java
281-
DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
282-
config.setPort(3316);
283-
ReusableDB db = ReusableDB.openEmbeddedDB(config.build());
281+
// Configuration handles pathing
282+
DBConfiguration config = mariaDBConfig.build();
283+
284+
// Logic handles reuse internally in 3.3.1+
285+
mariaDB = DB.newEmbeddedDB(config);
286+
mariaDB.start();

pom-step-01.xml

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616
<relativePath>pom.xml</relativePath>
1717
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.openmrs.api</groupId>
22-
<artifactId>openmrs-api</artifactId>
23-
<version>${openmrs.version}</version>
24-
<type>jar</type>
25-
<scope>provided</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.openmrs.distro</groupId>
29-
<artifactId>platform</artifactId>
30-
<version>${openmrs.version}</version>
31-
<type>war</type>
32-
<scope>provided</scope>
33-
</dependency>
34-
</dependencies>
19+
<dependencies>
20+
<dependency>
21+
<groupId>ch.vorburger.mariaDB4j</groupId>
22+
<artifactId>mariaDB4j</artifactId>
23+
<version>${mariadb4jVersion}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.openmrs.api</groupId>
28+
<artifactId>openmrs-api</artifactId>
29+
<version>${openmrs.version}</version>
30+
<type>jar</type>
31+
<scope>provided</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.openmrs.distro</groupId>
35+
<artifactId>platform</artifactId>
36+
<version>${openmrs.version}</version>
37+
<type>war</type>
38+
<scope>provided</scope>
39+
</dependency>
40+
</dependencies>
3541

3642
<build>
3743
<plugins>

pom-step-02.xml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@
1616
<relativePath>pom.xml</relativePath>
1717
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.openmrs.api</groupId>
22-
<artifactId>openmrs-api</artifactId>
23-
<version>${openmrs.version}</version>
24-
<type>jar</type>
25-
<scope>provided</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.openmrs.distro</groupId>
29-
<artifactId>platform</artifactId>
30-
<version>${openmrs.version}</version>
31-
<type>war</type>
32-
<scope>provided</scope>
33-
</dependency>
34-
</dependencies>
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>ch.vorburger.mariaDB4j</groupId>
23+
<artifactId>mariaDB4j</artifactId>
24+
<version>${mariadb4jVersion}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.openmrs.api</groupId>
29+
<artifactId>openmrs-api</artifactId>
30+
<version>${openmrs.version}</version>
31+
<type>jar</type>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.openmrs.distro</groupId>
36+
<artifactId>platform</artifactId>
37+
<version>${openmrs.version}</version>
38+
<type>war</type>
39+
<scope>provided</scope>
40+
</dependency>
41+
</dependencies>
3542

3643
<build>
3744
<plugins>

pom-step-03.xml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@
1616
<relativePath>pom.xml</relativePath>
1717
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.openmrs.api</groupId>
22-
<artifactId>openmrs-api</artifactId>
23-
<version>${openmrs.version}</version>
24-
<type>jar</type>
25-
<scope>provided</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.openmrs.distro</groupId>
29-
<artifactId>platform</artifactId>
30-
<version>${openmrs.version}</version>
31-
<type>war</type>
32-
<scope>provided</scope>
33-
</dependency>
34-
</dependencies>
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>ch.vorburger.mariaDB4j</groupId>
23+
<artifactId>mariaDB4j</artifactId>
24+
<version>${mariadb4jVersion}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.openmrs.api</groupId>
29+
<artifactId>openmrs-api</artifactId>
30+
<version>${openmrs.version}</version>
31+
<type>jar</type>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.openmrs.distro</groupId>
36+
<artifactId>platform</artifactId>
37+
<version>${openmrs.version}</version>
38+
<type>war</type>
39+
<scope>provided</scope>
40+
</dependency>
41+
</dependencies>
3542

3643
<build>
3744
<plugins>

pom-step-04.xml

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616
<relativePath>pom.xml</relativePath>
1717
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.openmrs.api</groupId>
22-
<artifactId>openmrs-api</artifactId>
23-
<version>${openmrs.version}</version>
24-
<type>jar</type>
25-
<scope>provided</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.openmrs.distro</groupId>
29-
<artifactId>platform</artifactId>
30-
<version>${openmrs.version}</version>
31-
<type>war</type>
32-
<scope>provided</scope>
33-
</dependency>
34-
</dependencies>
19+
<dependencies>
20+
<dependency>
21+
<groupId>ch.vorburger.mariaDB4j</groupId>
22+
<artifactId>mariaDB4j</artifactId>
23+
<version>${mariadb4jVersion}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.openmrs.api</groupId>
28+
<artifactId>openmrs-api</artifactId>
29+
<version>${openmrs.version}</version>
30+
<type>jar</type>
31+
<scope>provided</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.openmrs.distro</groupId>
35+
<artifactId>platform</artifactId>
36+
<version>${openmrs.version}</version>
37+
<type>war</type>
38+
<scope>provided</scope>
39+
</dependency>
40+
</dependencies>
3541

3642
<build>
3743
<plugins>

pom-step-05.xml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@
1616
<relativePath>pom.xml</relativePath>
1717
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.openmrs.api</groupId>
22-
<artifactId>openmrs-api</artifactId>
23-
<version>${openmrs.version}</version>
24-
<type>jar</type>
25-
<scope>provided</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.openmrs.distro</groupId>
29-
<artifactId>platform</artifactId>
30-
<version>${openmrs.version}</version>
31-
<type>war</type>
32-
<scope>provided</scope>
33-
</dependency>
34-
</dependencies>
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>ch.vorburger.mariaDB4j</groupId>
23+
<artifactId>mariaDB4j</artifactId>
24+
<version>${mariadb4jVersion}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.openmrs.api</groupId>
29+
<artifactId>openmrs-api</artifactId>
30+
<version>${openmrs.version}</version>
31+
<type>jar</type>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.openmrs.distro</groupId>
36+
<artifactId>platform</artifactId>
37+
<version>${openmrs.version}</version>
38+
<type>war</type>
39+
<scope>provided</scope>
40+
</dependency>
41+
</dependencies>
3542

3643
<build>
3744
<plugins>

pom.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
</modules>
2121

2222
<properties>
23-
<maven.compiler.source>1.8</maven.compiler.source>
24-
<maven.compiler.target>1.8</maven.compiler.target>
23+
24+
<maven.compiler.source>1.8</maven.compiler.source>
25+
<maven.compiler.target>1.8</maven.compiler.target>
26+
2527

2628
<ciel.dictionary.openmrs.version>2.5.5</ciel.dictionary.openmrs.version>
2729
<ciel.dictionary.version>20251224</ciel.dictionary.version>
@@ -37,7 +39,7 @@
3739
<tomcat.version>9.0.106</tomcat.version>
3840
<junitVersion>5.12.2</junitVersion>
3941
<mockitoVersion>3.12.4</mockitoVersion>
40-
<mariadb4jVersion>3.2.0</mariadb4jVersion>
42+
<mariadb4jVersion>3.3.1</mariadb4jVersion>
4143
</properties>
4244

4345
<dependencies>
@@ -107,6 +109,7 @@
107109
</dependency>
108110
</dependencies>
109111

112+
110113
<build>
111114
<finalName>openmrs-standalone-${openmrs.version}</finalName>
112115
<resources>

src/main/java/org/openmrs/standalone/MariaDbController.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import ch.vorburger.exec.ManagedProcessException;
2424
import ch.vorburger.mariadb4j.DB;
25+
import ch.vorburger.mariadb4j.DBConfiguration;
2526
import ch.vorburger.mariadb4j.DBConfigurationBuilder;
2627

2728
public class MariaDbController {
@@ -73,23 +74,19 @@ public static void startMariaDB(int port, String userPassword) throws Exception
7374
mariaDBConfig.addArg("--collation-server=utf8_general_ci");
7475
mariaDBConfig.addArg("--character-set-server=utf8");
7576

76-
if(isWindows){
77-
// For Windows, we use the ReusableDB class
78-
mariaDB = ReusableDB.openEmbeddedDB(mariaDBConfig.build());
79-
mariaDB.start();
77+
DBConfiguration config = mariaDBConfig.build();
78+
mariaDB = DB.newEmbeddedDB(config);
79+
mariaDB.start();
8080

81-
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:" + port + "/", ROOT_USER, ROOT_PASSWORD);
82-
try (Statement stmt = conn.createStatement()) {
81+
if (isWindows) {
82+
// Windows-specific setup using JDBC try-with-resources
83+
try (Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:" + port + "/", ROOT_USER, ROOT_PASSWORD);
84+
Statement stmt = conn.createStatement()) {
8385
stmt.execute("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + ROOT_PASSWORD + "';");
8486
stmt.execute("GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;");
8587
}
86-
8788
} else {
88-
// For Linux and macOS, we use the standard DB class
89-
mariaDB = DB.newEmbeddedDB(mariaDBConfig.build());
90-
mariaDB.start();
91-
92-
// Ensure root user exists and has correct password and privileges
89+
// Linux and macOS-specific setup
9390
mariaDB.run("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + ROOT_PASSWORD + "';");
9491
mariaDB.run("GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;");
9592
}

0 commit comments

Comments
 (0)