无需@Transactional注释的 Spring 管理事务
2022-09-01 23:47:07
						我正在使用Spring注释来管理我的交易,如下所示:
@Transactional(readOnly = true)
public class AlertServiceImpl implements AlertService {
     private AlertDAO alertDAO;
     public List<Alert> getAlerts(){
         List<Alert> alerts = alertDAO.getAlerts();
         return alerts;
     }
}
我想知道如果我忘记了注释会发生什么:
// Oops! Forgot to use transactional annotation 
public class AlertServiceImpl implements AlertService {
    private AlertDAO alertDAO;
    public List<Alert> getAlerts(){
         List<Alert> alerts = alertDAO.getAlerts();
         return alerts;
    }
}
当 alertDAO 实现如下时:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
// no annotation here either
public class HibernateAlertDAO extends HibernateDaoSupport implements AlertDAO {
    public List<Alert> getAlerts(){
         // some implementation details that define queryString
         Query query = getSession().createQuery(queryString);
         List<Alert> alerts = query.list();
         return alerts;
    }
}
似乎Hibernate允许我从数据库获取数据,即使没有注释。
这种粗心大意的后果是什么,可能发生的最坏情况是什么?