Connection connection=null; boolean rollback=false; try{ connection=dataSource.getConnection(); connection.setAutoCommit(false); //使用jdbc访问 connection.commit(); } catch(SQLException e){ e.printStackTrace(); rollback=true; } finally{ if(connection!=null) { if(rollback) { try{ connection.rollback(); } catch(SQLException e){ e.printStackTrace(); } else{ try { connection.close(); }catch(SQLException e){ e.printStackTrace(); } } } } }