Skip to content

Commit 908f59d

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

File tree

9 files changed

+186
-205
lines changed

9 files changed

+186
-205
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: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
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-core</artifactId>
23+
<version>3.3.1</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>ch.vorburger.mariaDB4j</groupId>
28+
<artifactId>mariaDB4j</artifactId>
29+
<version>${mariadb4jVersion}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.openmrs.api</groupId>
34+
<artifactId>openmrs-api</artifactId>
35+
<version>${openmrs.version}</version>
36+
<type>jar</type>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.openmrs.distro</groupId>
41+
<artifactId>platform</artifactId>
42+
<version>${openmrs.version}</version>
43+
<type>war</type>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
3547

3648
<build>
3749
<plugins>

pom-step-02.xml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
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-core</artifactId>
23+
<version>3.3.1</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>ch.vorburger.mariaDB4j</groupId>
28+
<artifactId>mariaDB4j</artifactId>
29+
<version>${mariadb4jVersion}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.openmrs.api</groupId>
34+
<artifactId>openmrs-api</artifactId>
35+
<version>${openmrs.version}</version>
36+
<type>jar</type>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.openmrs.distro</groupId>
41+
<artifactId>platform</artifactId>
42+
<version>${openmrs.version}</version>
43+
<type>war</type>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
3547

3648
<build>
3749
<plugins>

pom-step-03.xml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
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-core</artifactId>
23+
<version>3.3.1</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>ch.vorburger.mariaDB4j</groupId>
28+
<artifactId>mariaDB4j</artifactId>
29+
<version>${mariadb4jVersion}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.openmrs.api</groupId>
34+
<artifactId>openmrs-api</artifactId>
35+
<version>${openmrs.version}</version>
36+
<type>jar</type>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.openmrs.distro</groupId>
41+
<artifactId>platform</artifactId>
42+
<version>${openmrs.version}</version>
43+
<type>war</type>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
3547

3648
<build>
3749
<plugins>

pom-step-04.xml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
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-core</artifactId>
23+
<version>3.3.1</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>ch.vorburger.mariaDB4j</groupId>
28+
<artifactId>mariaDB4j</artifactId>
29+
<version>${mariadb4jVersion}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.openmrs.api</groupId>
34+
<artifactId>openmrs-api</artifactId>
35+
<version>${openmrs.version}</version>
36+
<type>jar</type>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.openmrs.distro</groupId>
41+
<artifactId>platform</artifactId>
42+
<version>${openmrs.version}</version>
43+
<type>war</type>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
3547

3648
<build>
3749
<plugins>

pom-step-05.xml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@
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-core</artifactId>
23+
<version>3.3.1</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>ch.vorburger.mariaDB4j</groupId>
28+
<artifactId>mariaDB4j</artifactId>
29+
<version>${mariadb4jVersion}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.openmrs.api</groupId>
34+
<artifactId>openmrs-api</artifactId>
35+
<version>${openmrs.version}</version>
36+
<type>jar</type>
37+
<scope>provided</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.openmrs.distro</groupId>
41+
<artifactId>platform</artifactId>
42+
<version>${openmrs.version}</version>
43+
<type>war</type>
44+
<scope>provided</scope>
45+
</dependency>
46+
</dependencies>
3547

3648
<build>
3749
<plugins>

pom.xml

Lines changed: 21 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,22 @@
107109
</dependency>
108110
</dependencies>
109111

112+
<dependencyManagement>
113+
<dependencies>
114+
<dependency>
115+
<groupId>ch.vorburger.mariaDB4j</groupId>
116+
<artifactId>mariaDB4j-core</artifactId>
117+
<version>${mariadb4jVersion}</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>ch.vorburger.mariaDB4j</groupId>
121+
<artifactId>mariaDB4j</artifactId>
122+
<version>${mariadb4jVersion}</version>
123+
</dependency>
124+
</dependencies>
125+
</dependencyManagement>
126+
127+
110128
<build>
111129
<finalName>openmrs-standalone-${openmrs.version}</finalName>
112130
<resources>

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

Lines changed: 8 additions & 6 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,9 +74,11 @@ 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());
77+
DBConfiguration config = mariaDBConfig.build();
78+
79+
if (isWindows) {
80+
// Check the dataDir variable that was defined above on line 66
81+
mariaDB = DB.newEmbeddedDB(config);
7982
mariaDB.start();
8083

8184
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:" + port + "/", ROOT_USER, ROOT_PASSWORD);
@@ -85,11 +88,10 @@ public static void startMariaDB(int port, String userPassword) throws Exception
8588
}
8689

8790
} else {
88-
// For Linux and macOS, we use the standard DB class
89-
mariaDB = DB.newEmbeddedDB(mariaDBConfig.build());
91+
// For Linux and macOS
92+
mariaDB = DB.newEmbeddedDB(config);
9093
mariaDB.start();
9194

92-
// Ensure root user exists and has correct password and privileges
9395
mariaDB.run("ALTER USER 'root'@'localhost' IDENTIFIED BY '" + ROOT_PASSWORD + "';");
9496
mariaDB.run("GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;");
9597
}

0 commit comments

Comments
 (0)