settings.setProperties(Maps.fromProperties(System.getProperties()));
boolean hasDialect = settings.hasKey("sonar.jdbc.dialect");
- if ((null == database) || (hasDialect)) { // Create database only once per vm (Only for in mempry database)
- if (hasDialect) {
- database = new DefaultDatabase(settings);
- } else {
- database = new H2Database();
- }
- database.start();
+ if (hasDialect) {
+ database = new DefaultDatabase(settings);
+ } else {
+ database = new H2Database();
+ }
+ database.start();
- myBatis = new MyBatis(database);
- myBatis.start();
+ myBatis = new MyBatis(database);
+ myBatis.start();
- databaseCommands = DatabaseCommands.forDialect(database.getDialect());
- }
+ databaseCommands = DatabaseCommands.forDialect(database.getDialect());
}
@Before
@BeforeClass
public static void startDatabase() throws Exception {
- if (null == database) { // Create only once per vm
- database = new H2Database();
- database.start();
+ database = new H2Database();
+ database.start();
- dbConnector = new MemoryDatabaseConnector(database);
- dbConnector.start();
+ dbConnector = new MemoryDatabaseConnector(database);
+ dbConnector.start();
- databaseCommands = DatabaseCommands.forDialect(database.getDialect());
- }
+ databaseCommands = DatabaseCommands.forDialect(database.getDialect());
}
@Before
@After
public void stopConnection() throws Exception {
- databaseTester.onTearDown();
- // Important: close the connection and session, otherwise tests can stuck
+ if (databaseTester != null) {
+ databaseTester.onTearDown();
+ }
if (connection != null) {
connection.close();
}
- session.stop();
+ if (session != null) {
+ session.stop();
+ }
}
public DatabaseSession getSession() {