jdbc mysql 缓存,Java MYSQL / JDBC查询从缓存的Connection返回过时数据

I've been searching Stackoverflow for an answer but can't seem to find one that doesn't involve Hibernate or some other database wrapper.

I'm using JDBC directly via the MYSQL 5.18 JDBC driver in a Tomcat 6 Java EE app. I am caching Connection objects, but not caching Statement objects. The ResultSets for the query are correctly returning up to date data on the first run. When I change a few rows via PHPMyAdmin or some other external tool, rerun the query, I get stale out-of-date data.

I'm using normal Statements, not PreparedStatements. I've tried ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE. I'm also closing out the result set. These do not solve the problem. I've also tried ResultSet.refreshRows(), but that results in an error because the query has a JOIN clause.

The only thing that clearly solves the problem is closing the Connection and reconnecting to the database, which results in a heavy cost for each query attempt.

Is there a way to reuse Connections without returning stale data?

EDIT: I'm not using transactions for queries at the moment.

Here's the general code.

Connection conn; //created elsewhere and reused

...

String query = "SELECT p.ID as oid,rid,handle,summary,city,state,zip,t.name AS category

FROM profiles AS p

JOIN (terms AS t) ON (p.tid = t.ID)

WHERE p.ID = 1";

ResultSet resultSet;

Statement s;

synchronized (conn)

{

s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

resultSet = s.executeQuery(query);

}

//Iterate over the results using .next() and copy data to another data structure

List retval = getResults(resultSet);

s.close();

Thanks for the help in advance!

解决方案

Turns out it was a matter of uncommited queries. Thanks to Brent Worden for the question about transactions which led me to look around and notice that I had disabled auto commit and was not committing after queries.

So the solutions that worked for me:

conn.setAutoCommit(true);

or

statement.executeQuery(query);

conn.commit();

This allows the queries to be flushed out and stale data is prevented.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JDBCMySQL数据库中查询数据的方法可以分为以下几步: 1. 加载驱动:使用Class.forName()方法加载MySQLJDBC驱动。 2. 获取连接:使用DriverManager.getConnection()方法获取数据库连接。 3. 创建Statement对象:使用Connection对象的createStatement()方法创建Statement对象。 4. 执行查询语句:使用Statement对象的executeQuery()方法执行查询语句,并将结果保在ResultSet对象中。 5. 处理结果集:使用ResultSet对象的getXXX()方法获取查询结果。 6. 关闭连接:使用Connection对象的close()方法关闭连接。 下面是一个简单的示例代码: ```java import java.sql.*; public class QueryDemo { public static void main(String[] args) { try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 获取连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); // 创建Statement对象 Statement stmt = conn.createStatement(); // 执行查询语句 String sql = "SELECT * FROM student"; ResultSet rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("id:" + id + ", name:" + name + ", age:" + age); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意:在使用完ResultSet、Statement和Connection对象后,需要及时关闭连接,以释放资源。另外,为了避免SQL注入等安全问题,应该使用PreparedStatement对象来执行带有参数的SQL语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值