Java 您好,如何用一个事务操作其他dao?
我有dao哪些方法应该在一个事务中正确执行它的最佳方法是什么? 汽车刀有如下方法Java 您好,如何用一个事务操作其他dao?,java,mysql,jdbc,dao,apache-commons-dbcp,Java,Mysql,Jdbc,Dao,Apache Commons Dbcp,我有dao哪些方法应该在一个事务中正确执行它的最佳方法是什么? 汽车刀有如下方法 public Car findCar(int numOfPas,String carCategory){ String query = "SELECT*FROM car_info WHERE numOfPas = ? AND carCategory=? AND carState='ready' ORDER BY RAND() LIMIT 1;"; Car foun
public Car findCar(int numOfPas,String carCategory){
String query = "SELECT*FROM car_info WHERE numOfPas = ? AND carCategory=? AND carState='ready' ORDER BY RAND() LIMIT 1;";
Car foundCar = null;
ResultSet resultSet = null;
try (Connection connection = MySQLDAOFactory.getConnection();
PreparedStatement statement = connection.prepareStatement(query)){
statement.setInt(1,numOfPas);
statement.setString(2,carCategory);
resultSet =statement.executeQuery();
if(resultSet.next()){
foundCar = new Car();
foundCar.setCarId(resultSet.getInt("carId"));
foundCar.setCarCategory(resultSet.getString("carCategory"));
foundCar.setNumOfPas(resultSet.getInt("numOfPas"));
foundCar.setCarState(resultSet.getString("carState"));
foundCar.setCarName(resultSet.getString("carName"));
foundCar.setCarImage(manager.byteToImage(resultSet.getBytes("carImage")));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return foundCar;
}
道的顺序如下
@Override
public boolean insertOrder(Order order) {
int rowNum = 0;
String query = "INSERT INTO user_order(userId,carId,userAddress,userDestination,orderCost,orderDate) values(?,?,?,?,?,?)";
ResultSet keys = null;
try (Connection connection = MySQLDAOFactory.getConnection();
PreparedStatement statement = connection.prepareStatement(query,Statement.RETURN_GENERATED_KEYS)){
statement.setInt(1,order.getUserId());
statement.setInt(2,order.getCarId());
statement.setString(3, order.getUserAddress());
statement.setString(4, order.getUserDestination());
statement.setDouble(5,order.getOrderCost());
statement.setTimestamp(6, Timestamp.valueOf(order.getOrderDate()));
rowNum = statement.executeUpdate();
keys = statement.getGeneratedKeys();
if(keys.next()){
order.setOrderId(keys.getInt(1));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return rowNum>0;
}
如何在一个事务中执行这些操作?我通过Apache dhcp连接池接收连接
编辑
这是一节课
我在哪里接电话
公共类MySQLDAOFactory扩展了DAOFactory{
public static Connection getConnection(){
Connection conn = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/UsersDB");
conn = ds.getConnection();
} catch (NamingException | SQLException e) {
e.printStackTrace();
}
return conn;
}
@Override
public CarDao getCarDao() {
return new MySQLCarDao();
}
@Override
public UserDao getUserDao() {
return new MySQLUserDao();
}
@Override
public OrderDao getOrderDao() {
return new MySQLOrderDao();
}
@Override
public CarCategoryDao getCarCategoryDao() {
return new MySQLCarCategoryDao();
}
}
管理事务有很多不同的方法。根据您的代码,最简单的方法是: 在
试一下块中:
在包装两个呼叫的呼叫者中创建连接
执行connection.setAutoCommit(false)
调用每个方法findCar()
和insertOrder()
调用connection.commit();
在catch
块中:
调用connection.rollback()
连接
不是在这些函数之外创建的,因此不要忘记从每个函数中删除连接设置。我通过提供更多信息编辑了文章