感谢回复:)我的web应用程序配置如grails 2.4.4版本,hibernate 4.3.6最终版本以及spring-core-4.0.7.RELEASE。
我表现得像
Session session2 = sessionFactory.getCurrentSession();
session2.persist(Obj);
session2.flush();
Transaction tx = session2.getTransaction();
log.debug("ITS CREATED??")
if(tx.isActive())
{
log.debug("ITS ACTIVE")
if(!tx.wasCommitted())
{
log.debug("NOT COMMITTED")
tx.commit();
}else
{
log.debug(" COMMITTED")
}
}else
{
log.debug("NOT ACTIVE")
}
当执行命中时,tx.commit()会抛出这种异常。
我在这里遗漏了什么吗?请指导我。
示例:
class MetaDataController
{
def saveService;
def save={
def returnParams = saveService.doSave(params);
//Here returning to the controller, its giving an exception.
}
}
class SaveService
{
def transactional = true;
def doSave(def params)
{
def params;
Session session2 = sessionFactory.getCurrentSession();
Metadata metaData = new MetaData();
///Object association
session2.persist(metaData);
session2.flush();
Transaction tx = session2.getTransaction();
if(!tx.wasCommitted())
{
log.debug("NOT COMMITTED")
tx.commit();
}
return params;
}
}
答案 0 :(得分:1)
对于这样一个简单的用例,您不需要处理会话和事务。您是否正在使用Grails,还记得吗?
class SaveService
{
def transactional = true;
def doSave(def params)
{
Metadata metaData = new MetaData()
metadata.save()
return params // I don't get this, but what the hell.
}
}