Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping PostgreSQL prepared statements in thread local variables #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/main/java/com/yugabyte/sample/apps/SqlInserts.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -50,13 +51,10 @@ public class SqlInserts extends AppBase {
private static final String DEFAULT_TABLE_NAME = "PostgresqlKeyValue";

// The shared prepared select statement for fetching the data.
private volatile PreparedStatement preparedSelect = null;
private ThreadLocal<PreparedStatement> preparedSelect = new ThreadLocal<PreparedStatement>();

// The shared prepared insert statement for inserting the data.
private volatile PreparedStatement preparedInsert = null;

// Lock for initializing prepared statement objects.
private static final Object prepareInitLock = new Object();
private ThreadLocal<PreparedStatement> preparedInsert = new ThreadLocal<PreparedStatement>();

public SqlInserts() {
buffer = new byte[appConfig.valueSize];
Expand Down Expand Up @@ -91,11 +89,34 @@ public String getTableName() {
}

private PreparedStatement getPreparedSelect() throws Exception {
if (preparedSelect == null) {
preparedSelect = getPostgresConnection().prepareStatement(
String.format("SELECT k, v FROM %s WHERE k = ?;", getTableName()));
if (preparedSelect.get() == null) {
preparedSelect.set(
getPostgresConnection().prepareStatement(
String.format("SELECT k, v FROM %s WHERE k = ?;", getTableName())));
}
return preparedSelect;
return preparedSelect.get();
}

private int readKey(Key key) throws Exception {
PreparedStatement statement = getPreparedSelect();
statement.setString(1, key.asString());
try (ResultSet rs = statement.executeQuery()) {
if (!rs.next()) {
LOG.error("Read key: " + key.asString() + " expected 1 row in result, got 0");
return 0;
}

if (!key.asString().equals(rs.getString("k"))) {
LOG.error("Read key: " + key.asString() + ", got " + rs.getString("k"));
}
LOG.debug("Read key: " + key.toString());

if (rs.next()) {
LOG.error("Read key: " + key.asString() + " expected 1 row in result, got more");
return 0;
}
}
return 1;
}

@Override
Expand All @@ -107,37 +128,19 @@ public long doRead() {
}

try {
PreparedStatement statement = getPreparedSelect();
statement.setString(1, key.asString());
try (ResultSet rs = statement.executeQuery()) {
if (!rs.next()) {
LOG.error("Read key: " + key.asString() + " expected 1 row in result, got 0");
return 0;
}

if (!key.asString().equals(rs.getString("k"))) {
LOG.error("Read key: " + key.asString() + ", got " + rs.getString("k"));
}
LOG.debug("Read key: " + key.toString());

if (rs.next()) {
LOG.error("Read key: " + key.asString() + " expected 1 row in result, got more");
return 0;
}
}
return readKey(key);
} catch (Exception e) {
LOG.fatal("Failed reading value: " + key.getValueStr(), e);
return 0;
}
return 1;
}

private PreparedStatement getPreparedInsert() throws Exception {
if (preparedInsert == null) {
preparedInsert = getPostgresConnection().prepareStatement(
String.format("INSERT INTO %s (k, v) VALUES (?, ?);", getTableName()));
if (preparedInsert.get() == null) {
preparedInsert.set(getPostgresConnection().prepareStatement(
String.format("INSERT INTO %s (k, v) VALUES (?, ?);", getTableName())));
}
return preparedInsert;
return preparedInsert.get();
}

@Override
Expand Down