conn.setAutoCommit(true)和(false)的区别

 默认的话为自动提交,
当没执行一个update ,delete或者insert的时候都会自动提交到数据库,无法回滚事务。
设置connection.setautocommit(false);只有程序调用connection.commit()的时候才会将先前执行的语句一起提交到数据库,这样就实现了数据库的事务。
 
true:sql命令的提交(commit)由驱动程序负责
false:sql命令的提交由应用程序负责,程序必须调用commit或者rollback方法
 
 
 
  • setAutoCommit
    void setAutoCommit(boolean autoCommit)
                       throws SQLException
    Sets this connection's auto-commit mode to the given state. If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method  commit or the method  rollback. By default, new connections are in auto-commit mode.

    The commit occurs when the statement completes. The time when the statement completes depends on the type of SQL Statement:

    • For DML statements, such as Insert, Update or Delete, and DDL statements, the statement is complete as soon as it has finished executing.
    • For Select statements, the statement is complete when the associated result set is closed.
    • For CallableStatement objects or for statements that return multiple results, the statement is complete when all of the associated result sets have been closed, and all update counts and output parameters have been retrieved.

    NOTE: If this method is called during a transaction and the auto-commit mode is changed, the transaction is committed. If setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.

    Parameters:
    autoCommit -  true to enable auto-commit mode;  false to disable it
    Throws:
    SQLException - if a database access error occurs, setAutoCommit(true) is called while participating in a distributed transaction, or this method is called on a closed connection
    See Also:
    getAutoCommit()

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值