diff options
author | Vitaly Takmazov | 2016-05-12 09:55:00 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-05-12 09:55:00 +0300 |
commit | c95b63af82ed58c68876563692340d74ced156f2 (patch) | |
tree | 6b77a602fa2b8629ce522b7ad39229a3463bf87c /src/main/java/com/juick/server/MessagesQueries.java | |
parent | 0a9cd317b878d6c2c777ec4eb19173a7d651abe5 (diff) |
fix reply id increment
Diffstat (limited to 'src/main/java/com/juick/server/MessagesQueries.java')
-rw-r--r-- | src/main/java/com/juick/server/MessagesQueries.java | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index c4919a8d..bfd0d71a 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -21,11 +21,14 @@ import com.juick.Message; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; @@ -119,8 +122,28 @@ public class MessagesQueries { } public static int getReplyIDIncrement(JdbcTemplate sql, int mid) { - sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); - return sql.queryForObject("SELECT maxreplyid FROM messages WHERE message_id=?", new Object[] {mid}, Integer.class); + return sql.execute((ConnectionCallback<Integer>) conn -> { + conn.setAutoCommit(false); + try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?")) { + ps.setInt(1, mid); + if (ps.executeUpdate() != 1) { + throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); + } + } + try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid FROM messages WHERE message_id=?")) { + ps.setInt(1, mid); + try (ResultSet resultSet = ps.executeQuery()) { + if (resultSet.next()) { + int rv = resultSet.getInt(1); + conn.commit(); + return rv; + } else { + throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0); + } + } + } + }); + } public static boolean recommendMessage(JdbcTemplate sql, int mid, int vuid) { |