/** * Commits a <code>Connection</code> then closes it, avoid closing if null * and hide any SQLExceptions that occur. * * @param conn Connection to close. */ public static void commitAndCloseQuietly(Connection conn) { try { commitAndClose(conn); } catch (SQLException e) { // NOPMD // quiet } }
/** * Commits and closes a connection. * @param conn Connection * @throws JuDbException If the commit and/or close fails */ public static void commitAndClose(Connection conn) throws JuDbException { try { log.debug("Commiting and closing connection [ID=" + JuObjectUtils.getIdentityString(conn) + "]"); DbUtils.commitAndClose(conn); } catch (SQLException ex) { throw new JuDbException("Couldn't commit and close connection", ex); } }
/** * Stores a "dataversion" record in the ES store. There is only a single one of these. The * return value of the add will include the version number of the entity. This version * number is what we use to determine whether our cache is stale. */ protected void updateDataVersion() { Connection conn = null; try { long newVersion = System.currentTimeMillis(); conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); run.update(conn, "DELETE FROM gw_dataversion"); //$NON-NLS-1$ run.update(conn, "INSERT INTO gw_dataversion (version) VALUES (?)", //$NON-NLS-1$ newVersion); DbUtils.commitAndClose(conn); dataVersion = newVersion; } catch (SQLException e) { dataVersion = -1; } }
/** * Stores a "dataversion" record in the ES store. There is only a single one of these. The * return value of the add will include the version number of the entity. This version * number is what we use to determine whether our cache is stale. */ protected void updateDataVersion() { Connection conn = null; try { long newVersion = System.currentTimeMillis(); conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); run.update(conn, "DELETE FROM gw_dataversion"); //$NON-NLS-1$ run.update(conn, "INSERT INTO gw_dataversion (version) VALUES (?)", //$NON-NLS-1$ newVersion); DbUtils.commitAndClose(conn); dataVersion = newVersion; } catch (SQLException e) { dataVersion = -1; } }
@Test public void commitAndCloseQuietly() throws Exception { Connection mockConnection = mock(Connection.class); DbUtils.commitAndClose(mockConnection); verify(mockConnection).commit(); verify(mockConnection).close(); }
@Test public void commitAndClose() throws Exception { Connection mockConnection = mock(Connection.class); DbUtils.commitAndClose(mockConnection); verify(mockConnection).commit(); verify(mockConnection).close(); }
@Test public void commitAndCloseWithException() throws Exception { Connection mockConnection = mock(Connection.class); doThrow(SQLException.class).when(mockConnection).commit(); try { DbUtils.commitAndClose(mockConnection); fail("DbUtils.commitAndClose() swallowed SQLEception!"); } catch (SQLException e) { // we expect this exception } verify(mockConnection).close(); }
/** * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void publishApi(Api api, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // First delete any record we might already have. run.update(conn, "DELETE FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion()); // Now insert a row for the api. String bean = mapper.writeValueAsString(api); run.update(conn, "INSERT INTO gw_apis (org_id, id, version, bean) VALUES (?, ?, ?, ?)", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null, Void.class)); } catch (SQLException | JsonProcessingException e) { handler.handle(AsyncResultImpl.create(e)); } }
/** * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void publishApi(Api api, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // First delete any record we might already have. run.update(conn, "DELETE FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion()); // Now insert a row for the api. String bean = mapper.writeValueAsString(api); run.update(conn, "INSERT INTO gw_apis (org_id, id, version, bean) VALUES (?, ?, ?, ?)", //$NON-NLS-1$ api.getOrganizationId(), api.getApiId(), api.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null, Void.class)); } catch (SQLException | JsonProcessingException e) { handler.handle(AsyncResultImpl.create(e)); } }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(Client client, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // Validate the client and populate the api map with apis found during validation. validateClient(client, conn); // Remove any old data first, then (re)insert run.update(conn, "DELETE FROM gw_clients WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ client.getOrganizationId(), client.getClientId(), client.getVersion()); String bean = mapper.writeValueAsString(client); run.update(conn, "INSERT INTO gw_clients (api_key, org_id, id, version, bean) VALUES (?, ?, ?, ?, ?)", //$NON-NLS-1$ client.getApiKey(), client.getOrganizationId(), client.getClientId(), client.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null)); } catch (Exception re) { DbUtils.rollbackAndCloseQuietly(conn); handler.handle(AsyncResultImpl.create(re, Void.class)); } }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(Client client, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // Validate the client and populate the api map with apis found during validation. validateClient(client, conn); // Remove any old data first, then (re)insert run.update(conn, "DELETE FROM gw_clients WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ client.getOrganizationId(), client.getClientId(), client.getVersion()); String bean = mapper.writeValueAsString(client); run.update(conn, "INSERT INTO gw_clients (api_key, org_id, id, version, bean) VALUES (?, ?, ?, ?, ?)", //$NON-NLS-1$ client.getApiKey(), client.getOrganizationId(), client.getClientId(), client.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null)); } catch (Exception re) { DbUtils.rollbackAndCloseQuietly(conn); handler.handle(AsyncResultImpl.create(re, Void.class)); } }