MySQL提交和回滚失败
我有一个问题。
try {
jdbcConnect( ); //get mysql connect
conn.setAutoCommit( false );
pstmt = conn.prepareStatement (
"INSERT INTO member ( member_name, member_introduce ) VALUES ( ?, ? )", Statement.RETURN_GENERATED_KEYS );
pstmt.setString( 1, "something" );
pstmt.setString( 2, "something" );
pstmt.executeUpdate( );
rs = pstmt.getGeneratedKeys( );
rs.next( );
String no = Integer.toString( rs.getInt( 1 );
pstmt = conn.prepareStatement ( "UPDATE account SET account_name = ? WHERE account_no = ?" );
pstmt.setString( 1, "something");
pstmt.setString( 2, no );
pstmt.executeUpdate( );
conn.commit( );
conn.setAutoCommit( true );
} catch ( SQLException t ) {
try {
if (conn != null) {
conn.rollback();
conn.setAutoCommit( true );
}
} catch ( SQLException e ) {
}
}//close conn and prepareStatement
我期待工作承诺。
但如果update语句发生错误,insert语句正在运行。
怎么了?
I have a problem.
try {
jdbcConnect( ); //get mysql connect
conn.setAutoCommit( false );
pstmt = conn.prepareStatement (
"INSERT INTO member ( member_name, member_introduce ) VALUES ( ?, ? )", Statement.RETURN_GENERATED_KEYS );
pstmt.setString( 1, "something" );
pstmt.setString( 2, "something" );
pstmt.executeUpdate( );
rs = pstmt.getGeneratedKeys( );
rs.next( );
String no = Integer.toString( rs.getInt( 1 );
pstmt = conn.prepareStatement ( "UPDATE account SET account_name = ? WHERE account_no = ?" );
pstmt.setString( 1, "something");
pstmt.setString( 2, no );
pstmt.executeUpdate( );
conn.commit( );
conn.setAutoCommit( true );
} catch ( SQLException t ) {
try {
if (conn != null) {
conn.rollback();
conn.setAutoCommit( true );
}
} catch ( SQLException e ) {
}
}//close conn and prepareStatement
I was expect working commit.
But If an error occurs update statement, insert statement is running.
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您使用的是 MyISAM 表而不是 InnoDB。 MyISAM 根本不支持事务。
I think that you are using MyISAM table instead of InnoDB. MyISAM does not support transaction at all.