程序很长,但是都知道这种程序意思很简单的:package testSetAutoCommit;import java.sql.*;public class TestSetAutoCommit {
private static final String DRIVER="com.mysql.jdbc.Driver";
static{try{
Class.forName(DRIVER);
}catch(Exception e){
e.printStackTrace();
}
}
private static final String URL="jdbc:mysql://localhost/mybbs?useUnicode=true&characterEncoding=gb2312";
private static final String USERNAME="root";
private static final String PASSWORD="root";
public static Connection getConnection(){
try{
Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn)
{
try{
if(rs!=null)
{
rs.close();
}
}catch(Exception e){}
try{
if(pst!=null)
{
pst.close();
}
}catch(Exception e){}
try{
if(conn!=null)
{
conn.close();
}
}catch(Exception e){}
}
public static void main(String []aa){
TestSetAutoCommit p=new TestSetAutoCommit();
Connection conn=p.getConnection();
String sql1="update board set boardName='5555' where boardId=20";
String sql2="update board set boardName='4444' where boardId=21";;
Statement stmt=null;
try {
conn.setAutoCommit(false);
stmt=conn.createStatement();
stmt.executeUpdate(sql1);
/*********************关键在于这句**************************/ Thread.sleep(6000);
/***********************************************/ stmt.executeUpdate(sql2);
conn.commit();
conn.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
try {
if(conn!=null){
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
if(stmt!=null){stmt.close();}
if(conn!=null){conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}问题: 都已经有了 Thread.sleep(6000);
意思明明是该在6秒钟以后 同时改变数据库的两条记录,
但是,在我不断刷新mysql的过程中,第一条记录一运行就改变了,第二条才是运行后6秒钟在改变,
这明明就不是conn.setAutoCommit(FALSE)该有的阻止自动提交的功能的嘛!
我觉得数据库中两条记录应该是6秒钟以后同时改变的呀!
private static final String DRIVER="com.mysql.jdbc.Driver";
static{try{
Class.forName(DRIVER);
}catch(Exception e){
e.printStackTrace();
}
}
private static final String URL="jdbc:mysql://localhost/mybbs?useUnicode=true&characterEncoding=gb2312";
private static final String USERNAME="root";
private static final String PASSWORD="root";
public static Connection getConnection(){
try{
Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn)
{
try{
if(rs!=null)
{
rs.close();
}
}catch(Exception e){}
try{
if(pst!=null)
{
pst.close();
}
}catch(Exception e){}
try{
if(conn!=null)
{
conn.close();
}
}catch(Exception e){}
}
public static void main(String []aa){
TestSetAutoCommit p=new TestSetAutoCommit();
Connection conn=p.getConnection();
String sql1="update board set boardName='5555' where boardId=20";
String sql2="update board set boardName='4444' where boardId=21";;
Statement stmt=null;
try {
conn.setAutoCommit(false);
stmt=conn.createStatement();
stmt.executeUpdate(sql1);
/*********************关键在于这句**************************/ Thread.sleep(6000);
/***********************************************/ stmt.executeUpdate(sql2);
conn.commit();
conn.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
try {
if(conn!=null){
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
if(stmt!=null){stmt.close();}
if(conn!=null){conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}问题: 都已经有了 Thread.sleep(6000);
意思明明是该在6秒钟以后 同时改变数据库的两条记录,
但是,在我不断刷新mysql的过程中,第一条记录一运行就改变了,第二条才是运行后6秒钟在改变,
这明明就不是conn.setAutoCommit(FALSE)该有的阻止自动提交的功能的嘛!
我觉得数据库中两条记录应该是6秒钟以后同时改变的呀!
解决方案 »
- ————lucene3.5 全文检索案例demo———0.5秒检索1000万数据
- struts2中的Dispatcher initialization failed问题
- ejb新手问题求助。
- Hibernate 延时加载问题!
- <logic:iterate> 如何遍历下面的内容
- 问个加密解密的问题
- 在STRUTS中怎么生成报表?
- java下socket和delphi socket程序连接的问题
- (即时给分)Weblogic6.0中不能运行Servlet,如何配置web.xml?
- Jbpm6 spring struts2 mybaits整合问题
- 路径加密。高手进来帮个忙
- 高分求教:struts2转换器出现异常
再补充一下我的问题:我根本就没有发现设置了conn.setAutoCommit(false/true)的任何作用,
因为后来我在第二条sql语句中个设置了一个错误,当然已经catch到了Exception,
按理说应该回滚,但是很失望,第一条语句在数据库中修改成功了,第二句却没有。怎么回事?急!急!
以上。