Setautocommit is set to false, finally closing the connection, will submit no

try {
			DBUtil.beginTranaction();
			dao.Add(emp);
			DBUtil.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				DBUtil.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			System.out.println("The system is busy, please try again later.");
		}


Begin transaction code:
public static void beginTranaction() throws SQLException{
		try {
			Connection conn = getConnection();
			System.out.println("What operation to there is no");
			conn.setAutoCommit(false);
			System.out.println("What run to here without false");
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("Start transaction failure");
			throw e;
		}
	}

The code for the add method:
	public void Add(Employee e) throws SQLException  {
		Connection conn=null;
		PreparedStatement prep=null;
		try{
		conn=DBUtil.getConnection();
		String sqlAdd="insert into t_emp(name,salary,age) values(?,?,?)";
		prep=conn.prepareStatement(sqlAdd);
		prep.setString(1,e.getName());
		prep.setDouble(2,e.getSalary());
		prep.setInt(3,e.getAge());
		int row=prep.executeUpdate();
		//System.out.println(row);
		}catch(SQLException ex){
			ex.printStackTrace();
			throw ex;
		}finally{
			if(prep!=null){
				prep.close();
			}
			DBUtil.close();
			System.out.println("The whole add connection was closed to why not insert");
		}
	}


I know is wrong, DBUtil.close add method (inside) removed can insert data, but I don't think I go can also insert data? Anomaly is behind a newspaper , DBUtil.commit();
Why you ask.

Started by Pearl at November 10, 2016 - 10:45 AM

I collapse. You put
DBUtil this object is closed. .
This object is equivalent to no use can be garbage collection mechanism to clear off. .
And then you want to call DBUtil to execute commit (); operation, may Mody. . .

Posted by Ashbur at November 16, 2016 - 11:35 AM

 }finally{
        if(prep!=null){
            prep.close();
        }
        DBUtil.close();
        System.out.println("The whole add connection was closed to why not insert");
    }

These two lines did not go wrong, you will DBUtil.close (); here performed, then to the next step.
DBUtil.commit();Nature is wrong

Posted by Lynn at November 17, 2016 - 11:38 AM

To prep.commit (DBUtil.commit) is not

Posted by Lori at December 01, 2016 - 11:57 AM