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

Hibernate: avoid potential deadlock on c3p0 #235

Open
wants to merge 1 commit into
base: master
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
70 changes: 31 additions & 39 deletions src/main/java/net/socialgamer/cah/data/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -703,47 +703,39 @@ else if (player.getScore() >= options.scoreGoal) {
* game is already started, or doesn't have enough cards, but hopefully callers and
* clients would prevent that from happening!
*/
public boolean start() {
Session session = null;
try {
session = sessionProvider.get();
if (state != GameState.LOBBY || !hasEnoughCards(session)) {
return false;
}
boolean started;
final int numPlayers = players.size();
if (numPlayers >= 3) {
// Pick a random start judge, though the "next" judge will actually go first.
judgeIndex = (int) (Math.random() * numPlayers);
started = true;
} else {
started = false;
}
if (started) {
currentUniqueId = uniqueIdProvider.get();
logger.info(String.format("Starting game %d with card sets %s, Cardcast %s, %d blanks, %d "
+ "max players, %d max spectators, %d score limit, players %s, unique %s.",
id, options.cardSetIds, cardcastDeckIds, options.blanksInDeck, options.playerLimit,
options.spectatorLimit, options.scoreGoal, players, currentUniqueId));
// do this stuff outside the players lock; they will lock players again later for much less
// time, and not at the same time as trying to lock users, which has caused deadlocks
final List<CardSet> cardSets;
synchronized (options.cardSetIds) {
cardSets = loadCardSets(session);
blackDeck = loadBlackDeck(cardSets);
whiteDeck = loadWhiteDeck(cardSets);
}
metrics.gameStart(currentUniqueId, cardSets, options.blanksInDeck, options.playerLimit,
options.scoreGoal, !StringUtils.isBlank(options.password));
startNextRound();
gameManager.broadcastGameListRefresh();
}
return started;
} finally {
if (null != session) {
session.close();
public boolean start(Session session) {
if (state != GameState.LOBBY || !hasEnoughCards(session)) {
return false;
}
boolean started;
final int numPlayers = players.size();
if (numPlayers >= 3) {
// Pick a random start judge, though the "next" judge will actually go first.
judgeIndex = (int) (Math.random() * numPlayers);
started = true;
} else {
started = false;
}
if (started) {
currentUniqueId = uniqueIdProvider.get();
logger.info(String.format("Starting game %d with card sets %s, Cardcast %s, %d blanks, %d "
+ "max players, %d max spectators, %d score limit, players %s, unique %s.",
id, options.cardSetIds, cardcastDeckIds, options.blanksInDeck, options.playerLimit,
options.spectatorLimit, options.scoreGoal, players, currentUniqueId));
// do this stuff outside the players lock; they will lock players again later for much less
// time, and not at the same time as trying to lock users, which has caused deadlocks
final List<CardSet> cardSets;
synchronized (options.cardSetIds) {
cardSets = loadCardSets(session);
blackDeck = loadBlackDeck(cardSets);
whiteDeck = loadWhiteDeck(cardSets);
}
metrics.gameStart(currentUniqueId, cardSets, options.blanksInDeck, options.playerLimit,
options.scoreGoal, !StringUtils.isBlank(options.password));
startNextRound();
gameManager.broadcastGameListRefresh();
}
return started;
}

public List<CardSet> loadCardSets(final Session session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Map<ReturnableData, Object> handleWithUserInGame(final RequestWrapper req
data.put(ErrorInformation.WHITE_CARDS_PRESENT, game.loadWhiteDeck(cardSets).totalCount());
data.put(ErrorInformation.WHITE_CARDS_REQUIRED, game.getRequiredWhiteCardCount());
return error(ErrorCode.NOT_ENOUGH_CARDS, data);
} else if (!game.start()) {
} else if (!game.start(hibernateSession)) {
return error(ErrorCode.NOT_ENOUGH_PLAYERS);
} else {
return data;
Expand Down