我的MySQL存储引擎是InnoDB。当我设置MySQL系统变量autocommit=0时,在执行这样的代码之后,我看不到数据库中的任何更改:
String sql = "insert into user (userName, userAge, password) " +
"values (?, ?, ?);";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "hh");
preparedStatement.setInt(2, 33);
preparedStatement.setString(3, "agadf");
int i = preparedStatement.executeUpdate();
conn.close();或者隐式地使用commit:
conn.setAutoCommit(false);
String sql = "insert into user (userName, userAge, password) " +
"values (?, ?, ?);";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "hh");
preparedStatement.setInt(2, 33);
preparedStatement.setString(3, "agadf");
int i = preparedStatement.executeUpdate();
conn.commit();
conn.setAutoCommit(true);执行此操作后,当我执行"select * from user;“时,无法看到任何更改。但是在我输入commit at MySQL shell之后,insert操作就可以工作了。
所以,我真的想知道系统变量autocommit的真正功能。如果我将其设置为,则无法使用JDBC修改数据而不执行MySQL shell中的commit。
同时,我想知道为什么当系统变量setAutomatic(true)或conn.commit()是时,不能将更改提交到真实的表中。
发布于 2021-04-15 10:04:51
从错误的角度来看,问题不是在JDBC中,而是在您的MySQL shell中。MySQL shell还使用事务(数据库中的所有操作都发生在事务中),如果您的MySQL配置为autocommit=0,那么如果您不在shell中提交,则shell将使用它的当前事务,它无法看到您所做的更改,因为该事务是在您进行更改之前启动的。在MySQL shell中提交会导致shell在选择之前启动一个新事务,因此它可以看到您在Java应用程序中所做的更改。
https://stackoverflow.com/questions/67104930
复制相似问题