在我的服务类,我想有这样的事情:休眠活动事务
class ClientService {
// Authorize
// Returns true: Authorization successful
// Returns false: Authorization failed
public boolean authorize(String id, String password) {
//Here I would like to check if an active transaction exists.
//If it exists, use that one, else create a new session and start
//a new transaction.
//For example:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
if(!session.SOMEMETHOD_CHECK_IF_TRANSACTION_IS_ACTIVE) {
session.beginTransaction();
}
Client client = clientDAO.get(id);
if (client != null) {
if (client.getPassword().equals(password)) {
logger.debug("Authorization successful. ID: " + client.getId() + ", password: " + client.getPassword());
return true;
} else {
logger.error("Authorization unsuccessful");
return false;
} else {
logger.debug("Authorization unsuccessful. No client exists with ID: " + id);
return false;
}
}
}
通知的方法头后的注释文本。我对Hibernate并不熟悉,但是我认为如果服务方法检查事务是否存在,使用它,否则创建一个新的并关闭它将会很好。
如果有可能,我是否应该考虑任何性能原因(或其他)?或者是否有任何其他方式来执行这种事情?
问候
我认为,session.getTransaction()肯定会返回一个活动的事务...所以不为我工作(atlest)....是否有任何其他解决方案? – 2013-07-17 06:30:13