我对hibernate还不熟悉,有一项任务是将用hibernate3编写的代码迁移到hibernate5。在调试时,我得到一个错误-java.lang.illegalstateexception:事务已经处于活动状态,并且这在方法-begintransaction()上发生。我在网上查了很多方法,但都无法纠正错误。下面是导致此错误的函数。
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public String callStoredProc(List<UniqueNoRidVo> uniqueNoRidVoMap) throws DBException {
List<UniqueNoRidVo> uniqueNo = null;
String return_value = null;
Transaction tx = null;
int count = 0;
Session session = sessionFactory.getCurrentSession();
try{
//Deleting old records from Temp table
LOG.info("IN callStoredProc -START");
String query = "delete from UniqueNoRidVo";
session.createQuery(query).executeUpdate();
LOG.info("Old records deleted from TEMP Table");
//Inserting New RIDs into Temp table
if(!uniqueNoRidVoMap.isEmpty()){
tx = session.beginTransaction(); //Error code
for(LiqVO vo : uniqueNoRidVoMap){
session.saveOrUpdate(vo);
if (++count % 50 == 0) {
session.flush();
session.clear();
}
}
session.flush();
LOG.info("New RIDs inserted into TEMP table");
}
//Calling Stored procedure to generate Unique Nos.
LOG.info("Calling Stored Procedure");
tx=session.getTransaction();
System.out.println("Calling Stored Procedure");
//migrated to hibernate 5
//SessionFactory sessionFactory=session.getSessionFactory();
ConnectionProvider
connectionProvider=liqSessionFactory.getSessionFactoryOptions().
getServiceRegistry().getService(ConnectionProvider.class);
Connection connection=connectionProvider.getConnection();
CallableStatement st = connection.prepareCall("{ call SP_FETCH_UQNO(?) }");
// CallableStatement st = session.connection().prepareCall("{ call SP_FETCH_UQNO(?) }");
st.registerOutParameter(1, OracleTypes.VARCHAR);
int abc = st.executeUpdate();
LOG.info("abc output"+abc);
if(abc>0) {
return_value = st.getString(1);
}
LOG.info("Stored Procedure call completed: return_value "+ return_value);
System.out.println("Stored Procedure call completed: return_value "+ return_value);
}catch(HibernateException hbex){
if (tx != null) {
tx.rollback();
LOG.error(hbex.toString());
System.out.println("GD004-Error occurred in getting Unique Numbers for RID"+
hbex.toString());
throw new DBException("GD004-Error occurred in getting Unique Numbers for RID",
hbex.toString());
}
} catch (SQLException sqex) {
LOG.error(sqex.toString());
throw new DBException("GD005-Error occurred in getting Unique Numbers for RID",
sqex.toString());
}
return return_value;
}
任何关于如何解决这个问题的建议,谢谢!
1条答案
按热度按时间xfb7svmp1#
您可以允许框架处理事务,而不是显式地打开事务,在spring中可以使用@transactional。
您甚至可以使用tr.isactive()方法检查事务是否仍然处于活动状态
理想情况下,不需要对事务执行任何操作,因为它是由spring处理的。
例如:
案例一:spring处理的事务
案例2:自行处理的交易