0

I have a DAO class, that has method below. I call this one inside Transaction manager. When I ran it without "conn.commit()" line - it throws timeout exception, but when I ran it with this one - it is ok. What's the problem? As I know it is not necessary to commit if you not modify db?

    @Override
    public List<String> getLinks(int id) throws SQLException {
        List<String> list = new ArrayList<>();
        Connection conn = factory.newConnection();
        Statement statement = null;
        ResultSet rs = null;
        try {
            String expression = "select link from users.links where id=" + id + " order by id_link desc";
            statement = conn.createStatement();
            rs = statement.executeQuery(expression);
            while (rs.next()) {
                list.add(rs.getString("link"));
            }
            // !!!!!!!!!!!!! without next line method throw TimeoutException
            conn.commit(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            return list;
        } catch (SQLException e) {
            rollBackQuietly(conn);
            e.printStackTrace();
        } finally {
            closeQuaitly(rs);
            closeQuaitly(statement);
            closeQuaitly(conn);
        }
        return null;
    }
Paul
  • 1
  • 1

1 Answers1

0

Seeing as the commit() call is after the line which is throwing the exception, this problem must come after repeated invocations of this method (useful information to include in your question). this leads me to believe that your Connection factory is re-using Connections, and that it is handing out "stale" Connections (Connections which have been sitting around for too long and are no longer usable). if this is all true, then you need to make your factory manage Connections better. if it is a reasonably built connection pool, it probably has some feature like "test while idle" or "test on get" which you need to enable.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118