/** * Gets a <b>non-pooled</b> connection, unless the user and password are the same as the default * values for this connection pool. * * @return A pooled connection. * @throws SQLException Occurs when no pooled connection is available, and a new physical * connection cannot be created. */ public Connection getConnection(String user, String password) throws SQLException { // If this is for the default user/password, use a pooled connection if (user == null || (user.equals(getUser()) && ((password == null && getPassword() == null) || (password != null && password.equals(getPassword()))))) { return getConnection(); } // Otherwise, use a non-pooled connection if (!initialized) { initialize(); } return super.getConnection(user, password); }
/** * Users of the connection must make sure to call {@link java.sql.Connection#close()} when done using it, * so that it is returned to the pool. * * @return a connection retrieved from the pool * @throws SQLException */ public Connection connection() throws SQLException { if (ds == null) { throw new IllegalStateException("DataSource not available"); } return ds.getConnection(); }
@Override public void deleteAllSessions(String name) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("DELETE FROM signal_session_store WHERE name = ?"); st.setString(1, name); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void removePreKey(int preKeyId) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("DELETE FROM signal_prekey_store WHERE prekey_id = ?"); st.setInt(1, preKeyId); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void removeSignedPreKey(int signedPreKeyId) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("DELETE FROM signal_signed_prekey_store WHERE signed_prekey_id = ?"); st.setInt(1, signedPreKeyId); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } } }
public void updateThread(ThreadInfo thread) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO thread_store (thread_id, message_expiration_time) VALUES (?, ?) ON CONFLICT (thread_id) DO UPDATE SET message_expiration_time = EXCLUDED.message_expiration_time;"); st.setString(1, thread.id); st.setInt(2, thread.messageExpirationTime); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
public void updateContact(ContactInfo contact) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO contacts_store (number, name, color) VALUES (?, ?, ?) ON CONFLICT (number) DO UPDATE SET name = EXCLUDED.name, color = EXCLUDED.color;"); st.setString(1, contact.number); st.setString(2, contact.name); st.setString(3, contact.color); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void storePreKey(int preKeyId, PreKeyRecord record) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO signal_prekey_store (prekey_id, record) VALUES (?, ?) ON CONFLICT (prekey_id) DO UPDATE SET record = EXCLUDED.record;"); st.setInt(1, preKeyId); st.setBytes(2, record.serialize()); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
public boolean exists(String toshiId) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT 1 FROM local_identities WHERE eth_address = ? LIMIT 1"); st.setString(1, toshiId); rs = st.executeQuery(); if (rs.next()) { return true; } return false; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public boolean containsSignedPreKey(int signedPreKeyId) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT 1 FROM signal_signed_prekey_store WHERE signed_prekey_id = ? LIMIT 1"); st.setInt(1, signedPreKeyId); rs = st.executeQuery(); if (rs.next()) { return true; } return false; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO signal_signed_prekey_store (signed_prekey_id, record) VALUES (?, ?) ON CONFLICT (signed_prekey_id) DO UPDATE SET record = EXCLUDED.record;"); st.setInt(1, signedPreKeyId); st.setBytes(2, record.serialize()); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public boolean containsPreKey(int preKeyId) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT 1 FROM signal_prekey_store WHERE prekey_id = ? LIMIT 1"); st.setInt(1, preKeyId); rs = st.executeQuery(); if (rs.next()) { return true; } return false; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public List<Integer> getSubDeviceSessions(String name) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT device_id FROM signal_session_store WHERE name = ? AND device_id != 1"); st.setString(1, name); rs = st.executeQuery(); List<Integer> results = new LinkedList<>(); while (rs.next()) { results.add(rs.getInt("device_id")); } return results; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void deleteSession(SignalProtocolAddress address) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("DELETE FROM signal_session_store WHERE name = ? AND device_id = ?"); st.setString(1, address.getName()); st.setInt(2, address.getDeviceId()); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT * FROM signal_prekey_store WHERE prekey_id = ? LIMIT 1"); st.setInt(1, preKeyId); rs = st.executeQuery(); while (rs.next()) { return new PreKeyRecord(rs.getBytes("record")); } return null; } catch (SQLException|IOException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
public List<ThreadInfo> getThreads() { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT * FROM thread_store"); rs = st.executeQuery(); List<ThreadInfo> threads = new LinkedList<>(); while (rs.next()) { ThreadInfo thread = new ThreadInfo(); thread.id = rs.getString("thread_id"); thread.messageExpirationTime = rs.getInt("message_expiration_time"); threads.add(thread); } return threads; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT * FROM signal_signed_prekey_store WHERE signed_prekey_id = ? LIMIT 1"); st.setInt(1, signedPreKeyId); rs = st.executeQuery(); while (rs.next()) { return new SignedPreKeyRecord(rs.getBytes("record")); } return null; } catch (SQLException|IOException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public void storeSession(SignalProtocolAddress address, SessionRecord record) { Connection conn = null; PreparedStatement st = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO signal_session_store (name, device_id, record) VALUES (?, ?, ?) ON CONFLICT (name, device_id) DO UPDATE SET record = EXCLUDED.record;"); st.setString(1, address.getName()); st.setInt(2, address.getDeviceId()); st.setBytes(3, record.serialize()); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public boolean containsSession(SignalProtocolAddress address) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("SELECT 1 FROM signal_session_store WHERE name = ? AND device_id = ?"); st.setString(1, address.getName()); st.setInt(2, address.getDeviceId()); rs = st.executeQuery(); if (rs.next()) { return true; } return false; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }
@Override public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = this.pool.getConnection(); st = conn.prepareStatement("INSERT INTO signal_identity_store (name, device_id, identity_key) VALUES (?, ?, ?) ON CONFLICT (name, device_id) DO UPDATE SET identity_key = EXCLUDED.identity_key, updated = true RETURNING updated;"); st.setString(1, address.getName()); st.setInt(2, address.getDeviceId()); st.setBytes(3, identityKey.serialize()); st.execute(); rs = st.getResultSet(); if (rs.next()) { return rs.getBoolean(1); } // NOTE: should always get a result here, so this should actually never be reached return false; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) {} } if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }