如何解决嗨,我如何通过一笔交易来操作其他dao?
我有 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 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();
}
}
解决方法
管理交易的方式有很多种。鉴于您的代码,最简单的方法是:
在 try
块中:
- 在包含两个调用的调用方中创建您的
connection
- 执行
connection.setAutoCommit(false)
- 调用每个方法
findCar()
和insertOrder()
- 致电
connection.commit();
在 catch
块中:
调用connection.rollback()
connection
不是在这些函数之外创建的,所以不要忘记从每个函数中删除连接设置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。