我有一个 DAO 类,它有下面的方法。我在事务管理器中称它为这个。当我在没有“conn.commit()”行的情况下运行它时 - 它会引发超时异常,但是当我用这个运行它时 - 没关系。有什么问题?据我所知,如果您不修改数据库,则无需提交?
@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;
}