@Before @BeforeEach public final void openTestConnections() { if(!suppressDb){ loadConfiguration("/database.properties"); List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); if (connectionWrappers.isEmpty()) { LOGGER.warn("no DB connections are configured, none opened"); return; } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if (rollback()){ db.openTransaction(); } } } }
@After @AfterEach public final void closeTestConnections() { if(!suppressDb){ List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { String dbName = connectionWrapper.getDbName(); DB db = new DB(dbName); if (rollback()) { db.rollbackTransaction(); } db.close(); } clearConnectionWrappers(); } } }
/** * Returns all connections which correspond dbName of this filter and not for testing * * @return all connections which correspond dbName of this filter and not for testing. */ private List<ConnectionSpecWrapper> getConnectionWrappers() { List<ConnectionSpecWrapper> allConnections = DbConfiguration.getConnectionSpecWrappers(); List<ConnectionSpecWrapper> result = new LinkedList<>(); for (ConnectionSpecWrapper connectionWrapper : allConnections) { if (!connectionWrapper.isTesting() && (dbName == null || dbName.equals(connectionWrapper.getDbName()))) result.add(connectionWrapper); } return result; } }
public static void openTestConnections(){ List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); if(connectionWrappers.isEmpty()){ LOGGER.warn("no DB connections are configured, none opened"); return; } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if (Configuration.rollback()) db.openTransaction(); } }
public static void closeTestConnections() { List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { String dbName = connectionWrapper.getDbName(); DB db = new DB(dbName); if (Configuration.rollback()) { db.rollbackTransaction(); } db.close(); } }
@Before public final void openTestConnections() { if(!suppressDb){ loadConfiguration("/database.properties"); List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); if (connectionWrappers.isEmpty()) { LOGGER.warn("no DB connections are configured, none opened"); return; } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if (rollback()){ db.openTransaction(); } } } }
@Override public void onException(Exception e) { if(Configuration.isTesting()) return; List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(); if (connectionWrappers != null && !connectionWrappers.isEmpty()) { for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); if (db.hasConnection()) { if (manageTransaction) { db.rollbackTransaction(); logDebug("Rolling back transaction due to exception: " + e); } db.close(); } } } }
@Override public void before() { if(Configuration.isTesting()) return; List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(); if (connectionWrappers.isEmpty()) { throw new InitException("There are no connection specs in '" + Configuration.getEnv() + "' environment"); } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if(manageTransaction){ db.openTransaction(); } } }
@Override public void after() { if(Configuration.isTesting()) return; List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(); if (connectionWrappers != null && !connectionWrappers.isEmpty()) { for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); if(db.hasConnection()){ if(manageTransaction){ if (RequestContext.exceptionHappened()) { logDebug("Skip commit transaction because already rolled back."); } else { db.commitTransaction(); } } db.close(); } } } }
@After public final void closeTestConnections() { if(!suppressDb){ List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { String dbName = connectionWrapper.getDbName(); DB db = new DB(dbName); if (rollback()) { db.rollbackTransaction(); } db.close(); } clearConnectionWrappers(); } } }