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

DDP-5479 addressing lockwait error by using same connection throughout. #81

Open
wants to merge 1 commit into
base: DDP-5479_StreamingGBF
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 11 additions & 19 deletions src/main/java/org/broadinstitute/dsm/db/DdpKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;

import static org.broadinstitute.ddp.db.TransactionWrapper.inTransaction;
Expand Down Expand Up @@ -47,30 +48,21 @@ public DdpKit(String DsmKitRequestId, String kitLabel, String trackingToId, Stri
this.CEOrdered = CEOrdered;
}

public void changeCEOrdered(boolean orderStatus) {
public void changeCEOrdered(Connection conn, boolean orderStatus) {
String query = "UPDATE ddp_kit SET CE_order = ? where dsm_kit_request_id = ?";
SimpleResult result = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setBoolean(1, orderStatus);
stmt.setString(2, this.getDsmKitRequestId());
int r = stmt.executeUpdate();
if (r != 1) {//number of subkits
throw new RuntimeException("Update query for CE order flag updated " + r + " rows! with dsm kit request id: " + this.getDsmKitRequestId());
}
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setBoolean(1, orderStatus);
stmt.setString(2, this.getDsmKitRequestId());
int r = stmt.executeUpdate();
if (r != 1) {//number of subkits
throw new RuntimeException("Update query for CE order flag updated " + r + " rows! with dsm kit request id: " + this.getDsmKitRequestId());
}
catch (Exception e) {
dbVals.resultException = e;
}
return dbVals;
});
if (result.resultException != null) {
throw new RuntimeException(result.resultException);
}
else {
logger.info("Updated CE_Order value for kit with dsm kit request id " + this.getDsmKitRequestId()
+ " to " + orderStatus);
}
catch (Exception e) {
throw new RuntimeException("Could not update ce_ordered status for " + this.getDsmKitRequestId(),e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private static void updateTrackingInfo(String statusType,

if (!isReturn) {
if (shouldTriggerEventForKitOnItsWayToParticipant(statusType, oldType)) {
KitDDPNotification kitDDPNotification = KitDDPNotification.getKitDDPNotification(SQL_SELECT_KIT_FOR_NOTIFICATION_EXTERNAL_SHIPPER + SELECT_BY_TRACKING_NUMBER, new String[] { DELIVERED, trackingId }, 2);//todo change this to the number of subkits but for now 2 for test boston works
KitDDPNotification kitDDPNotification = KitDDPNotification.getKitDDPNotification(conn,SQL_SELECT_KIT_FOR_NOTIFICATION_EXTERNAL_SHIPPER + SELECT_BY_TRACKING_NUMBER, new String[] { DELIVERED, trackingId }, 2);//todo change this to the number of subkits but for now 2 for test boston works
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing through conn in a few places that I missed earlier.

if (kitDDPNotification != null) {
logger.info("Triggering DDP for kit going to participant with external order number: " + kit.getExternalOrderNumber());
EventUtil.triggerDDP(conn, kitDDPNotification);
Expand All @@ -210,15 +210,15 @@ private static void updateTrackingInfo(String statusType,
// using the earliest date of inbound event
orderRegistrar.orderTest(DSMServer.careEvolveAuth, kit.getHRUID(), kit.getKitLabel(), kit.getExternalOrderNumber(), earliestInTransitTime);
logger.info("Placed CE order for kit with external order number " + kit.getExternalOrderNumber());
kit.changeCEOrdered(true);
kit.changeCEOrdered(conn,true);
}
else {
logger.info("No return events for " + kit.getKitLabel() + ". Will not place order yet.");
}
if (shouldTriggerEventForReturnKitDelivery(statusType, oldType)) {
KitUtil.setKitReceived(kit.getKitLabel());
KitUtil.setKitReceived(conn, kit.getKitLabel());
logger.info("RECEIVED: " + trackingId);
KitDDPNotification kitDDPNotification = KitDDPNotification.getKitDDPNotification(SQL_SELECT_KIT_FOR_NOTIFICATION_EXTERNAL_SHIPPER + SELECT_BY_RETURN_NUMBER, new String[] { RECEIVED, trackingId }, 2);//todo change this to the number of subkits but for now 2 for test boston works
KitDDPNotification kitDDPNotification = KitDDPNotification.getKitDDPNotification(conn,SQL_SELECT_KIT_FOR_NOTIFICATION_EXTERNAL_SHIPPER + SELECT_BY_RETURN_NUMBER, new String[] { RECEIVED, trackingId }, 2);//todo change this to the number of subkits but for now 2 for test boston works
if (kitDDPNotification != null) {
logger.info("Triggering DDP for received kit with external order number: " + kit.getExternalOrderNumber());
EventUtil.triggerDDP(conn, kitDDPNotification);
Expand Down
59 changes: 26 additions & 33 deletions src/main/java/org/broadinstitute/dsm/model/KitDDPNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

Expand Down Expand Up @@ -86,41 +87,33 @@ public static KitDDPNotification getKitDDPNotification(@NonNull String query, @N
return (KitDDPNotification) results.resultValue;
}

public static KitDDPNotification getKitDDPNotification(@NonNull String query, @NonNull String[] inputs, int expectedCount) {
SimpleResult results = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
int counter = 1;
for (String string : inputs) {
stmt.setString(counter, string);
counter++;
}
try (ResultSet rs = stmt.executeQuery()) {
rs.last();
int count = rs.getRow();
rs.beforeFirst();
if (count == expectedCount && rs.next()) { //if row is 0 the ddp/kit type combination does not trigger a participant event
dbVals.resultValue = new KitDDPNotification(rs.getString(DBConstants.DDP_PARTICIPANT_ID),
rs.getString(DBConstants.DSM_KIT_REQUEST_ID), rs.getString(DBConstants.DDP_INSTANCE_ID),
rs.getString(DBConstants.INSTANCE_NAME),
rs.getString(DBConstants.BASE_URL),
rs.getString(DBConstants.EVENT_NAME),
rs.getString(DBConstants.EVENT_TYPE), rs.getLong(DBConstants.DSM_RECEIVE_DATE),
rs.getBoolean(DBConstants.NEEDS_AUTH0_TOKEN),
rs.getString(DBConstants.UPLOAD_REASON),
rs.getString(DBConstants.DDP_KIT_REQUEST_ID));
}
}
public static KitDDPNotification getKitDDPNotification(Connection conn, @NonNull String query, @NonNull String[] inputs, int expectedCount) {
KitDDPNotification result = null;
try (PreparedStatement stmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
int counter = 1;
for (String string : inputs) {
stmt.setString(counter, string);
counter++;
}
catch (Exception ex) {
dbVals.resultException = ex;
try (ResultSet rs = stmt.executeQuery()) {
rs.last();
int count = rs.getRow();
rs.beforeFirst();
if (count == expectedCount && rs.next()) { //if row is 0 the ddp/kit type combination does not trigger a participant event
result = new KitDDPNotification(rs.getString(DBConstants.DDP_PARTICIPANT_ID),
rs.getString(DBConstants.DSM_KIT_REQUEST_ID), rs.getString(DBConstants.DDP_INSTANCE_ID),
rs.getString(DBConstants.INSTANCE_NAME),
rs.getString(DBConstants.BASE_URL),
rs.getString(DBConstants.EVENT_NAME),
rs.getString(DBConstants.EVENT_TYPE), rs.getLong(DBConstants.DSM_RECEIVE_DATE),
rs.getBoolean(DBConstants.NEEDS_AUTH0_TOKEN),
rs.getString(DBConstants.UPLOAD_REASON),
rs.getString(DBConstants.DDP_KIT_REQUEST_ID));
}
}
return dbVals;
});

if (results.resultException != null) {
logger.error("Failed to get KitDDPNotification w/ input " + inputs[inputs.length-1], results.resultException);
} catch (Exception ex) {
logger.error("Failed to get KitDDPNotification w/ input " + inputs[inputs.length - 1], ex);
}
return (KitDDPNotification) results.resultValue;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Object getBSPKitInfo(Connection conn, @NonNull String kitLabel, @NonNull
return null;
}

boolean firstTimeReceived = KitUtil.setKitReceived(kitLabel);
boolean firstTimeReceived = KitUtil.setKitReceived(conn, kitLabel);
if (StringUtils.isNotBlank(bspKitInfo.getParticipantExitId())) {
String message = "Kit of exited participant " + bspKitInfo.getBspParticipantId() + " was received by GP.<br>";
notificationUtil.sentNotification(bspKitInfo.getNotificationRecipient(), message, NotificationUtil.DSM_SUBJECT);
Expand Down
43 changes: 18 additions & 25 deletions src/main/java/org/broadinstitute/dsm/util/KitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,34 +348,27 @@ public static void updateCollaboratorIds(@NonNull KitRequestCreateLabel kitLabel
}
}

public static boolean setKitReceived(String kitLabel) {
SimpleResult results = inTransaction((conn) -> {
SimpleResult dbVals = new SimpleResult();
try (PreparedStatement stmt = conn.prepareStatement(SQL_UPDATE_KIT_RECEIVED)) {
stmt.setLong(1, System.currentTimeMillis());
stmt.setString(2, BSP);
stmt.setString(3, kitLabel);
int result = stmt.executeUpdate();
if (result > 1) { // 1 row or 0 row updated is perfect
throw new RuntimeException("Error updating kit w/label " + kitLabel + " (was updating " + result + " rows)");
}
if (result == 1) {
dbVals.resultValue = true;
}
else {
dbVals.resultValue = false;
}
public static boolean setKitReceived(Connection conn, String kitLabel) {
boolean resultBoolean = false;
try (PreparedStatement stmt = conn.prepareStatement(SQL_UPDATE_KIT_RECEIVED)) {
stmt.setLong(1, System.currentTimeMillis());
stmt.setString(2, BSP);
stmt.setString(3, kitLabel);
int result = stmt.executeUpdate();
if (result > 1) { // 1 row or 0 row updated is perfect
throw new RuntimeException("Error updating kit w/label " + kitLabel + " (was updating " + result + " rows)");
}
catch (Exception ex) {
dbVals.resultException = ex;
if (result == 1) {
resultBoolean = true;
}
return dbVals;
});
if (results.resultException != null) {
logger.error("Failed to set kit w/ label " + kitLabel + " as received ", results.resultException);
return false;
else {
resultBoolean = false;
}
}
catch (Exception e) {
logger.error("Failed to set kit w/ label " + kitLabel + " as received ", e);
}
return (boolean) results.resultValue;
return resultBoolean;
}

public static void getKitStatus() {
Expand Down