Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java: [8]
java: [8, 17, 21]
steps:
- name: Checkout source
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
<jdk>[1.8,)</jdk>
</activation>
<properties>
<lombok.version>1.18.30</lombok.version>
<additionalparam>-Xdoclint:none</additionalparam>
</properties>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,34 @@ public void otherMethod(){
List<String> list = myDbEnvironment.getDatabaseNames();//取得当前环境下数据库名称列表
System.out.println(list);

// JE requires the handle to be closed before truncation. Reopen it so
// this template remains usable after clearing the records.
com.sleepycat.je.DatabaseConfig config = myDatabase.getConfig();
myDatabase.close();
long deleteNum = env.truncateDatabase(null, databaseName, true);
myDatabase = env.openDatabase(null, databaseName, config);
// Delete in a transaction while keeping the shared Database handle open.
Transaction txn = env.beginTransaction(null, null);
Cursor cursor = null;
long deleteNum = 0;
boolean committed = false;
try {
cursor = myDatabase.openCursor(txn, null);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
cursor.delete();
deleteNum++;
}
cursor.close();
cursor = null;
txn.commit();
committed = true;
} finally {
try {
if (cursor != null) {
cursor.close();
}
} finally {
if (!committed) {
txn.abort();
}
}
}
System.out.println(deleteNum);


Expand Down
21 changes: 19 additions & 2 deletions src/test/java/com/sleepycat/berkeley/spring/boot/BerkeleyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class BerkeleyTest {
@TempDir Path home;
Expand All @@ -40,6 +41,7 @@ class BerkeleyTest {
void openDatabase() {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setConfigParam("je.freeDisk", "0");
envConfig.setTransactional(true);
environment = new Environment(home.toFile(), envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
Expand Down Expand Up @@ -80,12 +82,27 @@ void storesPrimitiveValues() {


@Test
void truncatesAndReopensTheDatabase() {
void clearsAllRecordsWithoutReplacingSharedHandle() {
template.writeToDatabase("keep", "before", true);
template.writeToDatabase("second", "before", true);
template.otherMethod();
database = template.getDatabase();
assertSame(database, template.getDatabase());
assertEquals("", template.readFromDatabase("keep"));
assertEquals("", template.readFromDatabase("second"));
template.writeToDatabase("after", "value", true);
assertEquals("value", template.readFromDatabase("after"));
}
@Test
void failedClearLeavesTheSharedHandleUntouched() {
Database failing = mock(Database.class);
when(failing.getDatabaseName()).thenReturn("items");
when(failing.getEnvironment()).thenReturn(environment);
when(failing.openCursor(any(com.sleepycat.je.Transaction.class), isNull())).thenThrow(new IllegalStateException("cursor unavailable"));
template.setDatabase(failing);
assertThrows(IllegalStateException.class, () -> template.otherMethod());
assertSame(failing, template.getDatabase());
template.setDatabase(database);
template.writeToDatabase("after-failure", "valid", true);
assertEquals("valid", template.readFromDatabase("after-failure"));
}
}
Loading