77package com .ibm .fhir .database .utils .derby ;
88
99import java .io .File ;
10+ import java .io .FileNotFoundException ;
1011import java .io .IOException ;
12+ import java .io .PrintWriter ;
1113import java .sql .Connection ;
1214import java .sql .DriverManager ;
1315import java .sql .SQLException ;
2830 * Set up an instance of Derby for use with unit tests
2931 */
3032public 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
0 commit comments