Skip to content

Commit 91dea8c

Browse files
issue #426 fix for derby jdbc pipeline error
Signed-off-by: Albert Wang <xuwang@us.ibm.com>
1 parent 2243ded commit 91dea8c

3 files changed

Lines changed: 70 additions & 28 deletions

File tree

fhir-database-utils/src/main/java/com/ibm/fhir/database/utils/derby/DerbyMaster.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package com.ibm.fhir.database.utils.derby;
88

99
import java.io.File;
10+
import java.io.FileNotFoundException;
1011
import java.io.IOException;
12+
import java.io.PrintWriter;
1113
import java.sql.Connection;
1214
import java.sql.DriverManager;
1315
import java.sql.SQLException;
@@ -28,17 +30,23 @@
2830
* Set up an instance of Derby for use with unit tests
2931
*/
3032
public class DerbyMaster implements AutoCloseable {
31-
33+
3234
private static final Logger logger = Logger.getLogger(DerbyMaster.class.getName());
33-
35+
3436
// The directory holding our derby databases
3537
private static final String DERBY_DIR = "derby/";
3638

39+
// The derby properties file
40+
private static final String DERBY_PROPERTIES = DERBY_DIR + "derby.properties";
41+
3742
// The translator to help us out with Derby syntax
3843
private static final IDatabaseTranslator DERBY_TRANSLATOR = new DerbyTranslator();
39-
44+
4045
// The name of the database we manage
41-
private final String database;
46+
private final String database;
47+
48+
// Controls if we run derby in debugging mode which enables more logs.
49+
private final boolean debugging = false;
4250

4351
/**
4452
* Public constructor
@@ -47,18 +55,28 @@ public class DerbyMaster implements AutoCloseable {
4755
*/
4856
public DerbyMaster(String database) {
4957
this.database = database;
50-
51-
try {
58+
59+
try (PrintWriter out = new PrintWriter(DERBY_PROPERTIES)){
5260
Class.forName(DERBY_TRANSLATOR.getDriverClassName());
61+
// This speeds up sequence fetching by pre-creating 1000 instead of the default 100.
62+
out.println("derby.language.sequence.preallocator=1000");
63+
if (debugging) {
64+
out.println("derby.language.logQueryPlan=true");
65+
out.println("derby.language.logStatementText=true");
66+
out.println("derby.locks.deadlockTrace=true");
67+
out.println("derby.infolog.append=true");
68+
}
5369
}
5470
catch (ClassNotFoundException e) {
5571
throw new IllegalStateException(e);
72+
} catch (FileNotFoundException e1) {
73+
logger.warning("Failed to create derby.properties file!");
5674
}
57-
75+
5876
dropDatabase(database);
59-
77+
6078
}
61-
79+
6280
/**
6381
* Drop the contents of the database on disk. Must reside in the derby/ directory
6482
* as a simple check against accidentally wiping the wrong files
@@ -68,13 +86,13 @@ public static void dropDatabase(String database) {
6886
if (!database.startsWith(DERBY_DIR)) {
6987
throw new IllegalArgumentException("Derby databases must start with: " + DERBY_DIR);
7088
}
71-
89+
7290
try {
7391
File dir = new File(database);
7492
if (dir.exists()) {
7593
delete(dir);
7694
}
77-
}
95+
}
7896
catch (IOException e) {
7997
throw new IllegalStateException("Failed to delete derby DB: " + database, e);
8098
}
@@ -92,7 +110,7 @@ private static void delete(File file) throws IOException {
92110
//directory is empty, then delete it
93111
if (file.list().length == 0) {
94112
file.delete();
95-
}
113+
}
96114
else {
97115
// list all the directory contents
98116
String files[] = file.list();
@@ -110,7 +128,7 @@ private static void delete(File file) throws IOException {
110128
file.delete();
111129
}
112130
}
113-
}
131+
}
114132
else {
115133
// if file exists, then delete it
116134
file.delete();
@@ -126,7 +144,7 @@ private static void delete(File file) throws IOException {
126144
public Connection createConnection() throws SQLException {
127145
logger.info("Opening connection to Derby database: " + database);
128146
Connection connection;
129-
try {
147+
try {
130148
// Make sure the Derby driver is loaded
131149
Properties properties = new Properties();
132150
DerbyPropertyAdapter adapter = new DerbyPropertyAdapter(properties);
@@ -136,7 +154,7 @@ public Connection createConnection() throws SQLException {
136154
}
137155
catch (SQLException x) {
138156
throw DERBY_TRANSLATOR.translate(x);
139-
}
157+
}
140158

141159
return connection;
142160
}
@@ -148,7 +166,7 @@ public Connection createConnection() throws SQLException {
148166
public IDatabaseTranslator getTranslator() {
149167
return DERBY_TRANSLATOR;
150168
}
151-
169+
152170
/**
153171
* Ask the schema to apply itself to our target (adapter pattern)
154172
* @param pdm
@@ -167,7 +185,7 @@ public void runWithAdapter(java.util.function.Consumer<IDatabaseAdapter> fn) {
167185
try (Connection c = createConnection()) {
168186
try {
169187
JdbcTarget target = new JdbcTarget(c);
170-
188+
171189
if (logger.isLoggable(Level.FINE)) {
172190
// Decorate the target so that we print all the DDL before executing
173191
PrintTarget printer = new PrintTarget(target, logger.isLoggable(Level.FINE));
@@ -190,7 +208,7 @@ public void runWithAdapter(java.util.function.Consumer<IDatabaseAdapter> fn) {
190208
catch (SQLException x) {
191209
throw DERBY_TRANSLATOR.translate(x);
192210
}
193-
211+
194212
}
195213

196214
@Override

fhir-persistence-jdbc/src/test/java/com/ibm/fhir/persistence/jdbc/test/spec/R4JDBCExamplesTest.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66

77
package com.ibm.fhir.persistence.jdbc.test.spec;
88

9+
import java.util.ArrayList;
10+
import java.util.List;
911
import java.util.Properties;
1012

1113
import org.testng.annotations.AfterClass;
1214
import org.testng.annotations.BeforeSuite;
1315
import org.testng.annotations.Test;
1416

17+
import com.ibm.fhir.database.utils.api.ITransactionProvider;
18+
import com.ibm.fhir.database.utils.pool.PoolConnectionProvider;
19+
import com.ibm.fhir.database.utils.transaction.SimpleTransactionProvider;
1520
import com.ibm.fhir.model.spec.test.R4ExamplesDriver;
1621
import com.ibm.fhir.model.spec.test.R4ExamplesDriver.TestType;
1722
import com.ibm.fhir.model.test.TestUtil;
1823
import com.ibm.fhir.persistence.FHIRPersistence;
1924
import com.ibm.fhir.persistence.context.FHIRHistoryContext;
2025
import com.ibm.fhir.persistence.context.FHIRPersistenceContext;
2126
import com.ibm.fhir.persistence.context.FHIRPersistenceContextFactory;
22-
import com.ibm.fhir.persistence.jdbc.impl.FHIRPersistenceJDBCImpl;
2327
import com.ibm.fhir.persistence.test.common.AbstractPersistenceTest;
2428
import com.ibm.fhir.schema.derby.DerbyFhirDatabase;
2529

@@ -44,10 +48,29 @@ public void bootstrapAndLoad() {
4448

4549
@Test(groups = { "jdbc-seed" })
4650
public void perform() throws Exception {
47-
48-
R4JDBCExamplesProcessor processor = new R4JDBCExamplesProcessor(persistence,
49-
() -> createPersistenceContext(),
50-
() -> createHistoryPersistenceContext());
51+
// Use connection pool and transaction provider to make sure the resource operations
52+
// of each resource are committed after the processing is finished, and because this
53+
// testng test process the samples one by one, so set the connection pool size to 1.
54+
PoolConnectionProvider connectionPool = new PoolConnectionProvider(database, 1);
55+
ITransactionProvider transactionProvider = new SimpleTransactionProvider(connectionPool);
56+
List<ITestResourceOperation> operations = new ArrayList<>();
57+
operations.add(new CreateOperation());
58+
operations.add(new ReadOperation());
59+
operations.add(new UpdateOperation());
60+
operations.add(new UpdateOperation());
61+
operations.add(new ReadOperation());
62+
operations.add(new VReadOperation());
63+
operations.add(new HistoryOperation(3));
64+
operations.add(new DeleteOperation());
65+
operations.add(new DeleteOperation());
66+
operations.add(new HistoryOperation(4));
67+
R4JDBCExamplesProcessor processor = new R4JDBCExamplesProcessor(
68+
operations,
69+
this.properties,
70+
connectionPool,
71+
null,
72+
null,
73+
transactionProvider);
5174

5275
// Overriding the JDBC ALL to Minimal.
5376
// Unless the profile tells us differently
@@ -92,7 +115,8 @@ protected FHIRPersistenceContext createHistoryPersistenceContext() {
92115

93116
@Override
94117
public FHIRPersistence getPersistenceImpl() throws Exception {
95-
return new FHIRPersistenceJDBCImpl(this.properties, database);
118+
//return new FHIRPersistenceJDBCImpl(this.properties, database);
119+
return null;
96120
}
97121

98122
@Override

fhir-persistence/src/test/java/com/ibm/fhir/persistence/test/common/AbstractPersistenceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public void setUp() throws Exception {
8686

8787
@BeforeMethod(alwaysRun = true)
8888
public void startTrx() throws Exception{
89-
if (persistence.isTransactional()) {
89+
if (persistence != null && persistence.isTransactional()) {
9090
persistence.getTransaction().begin();
9191
}
9292
}
9393

9494
@AfterMethod(alwaysRun = true)
9595
public void commitTrx() throws Exception{
96-
if (persistence.isTransactional()) {
96+
if (persistence != null && persistence.isTransactional()) {
9797
persistence.getTransaction().commit();
9898
}
9999
}
@@ -117,11 +117,11 @@ protected List<Resource> runQueryTest(Class<? extends Resource> resourceType, St
117117
protected List<Resource> runQueryTest(Class<? extends Resource> resourceType, Map<String, List<String>> queryParms) throws Exception {
118118
return runQueryTest(resourceType, queryParms, null);
119119
}
120-
120+
121121
protected List<Resource> runQueryTest(Class<? extends Resource> resourceType, Map<String, List<String>> queryParms, Integer maxPageSize) throws Exception {
122122
return runQueryTest(SearchUtil.parseQueryParameters(resourceType, queryParms), resourceType, queryParms, maxPageSize).getResource();
123123
}
124-
124+
125125
protected MultiResourceResult<Resource> runQueryTest(FHIRSearchContext searchContext, Class<? extends Resource> resourceType, Map<String, List<String>> queryParms, Integer maxPageSize) throws Exception {
126126
// ensure that all the query parameters were processed into search parameters (needed because the server ignores invalid params by default)
127127
int expectedCount = 0;

0 commit comments

Comments
 (0)