gpt4 book ai didi

java - Hibernate + Jersey + Jackson 随机获取 “org.hibernate.TransactionException: nested transactions not supported”

转载 作者:行者123 更新时间:2023-12-02 15:44:05 24 4
gpt4 key购买 nike

我已经使用这些技术 + c3p0 构建了一个 Web 服务来进行数据库处理。大多数时候它工作正常,但由于此错误,我有 3-5%(有时甚至 10%)的访问失败。

我以这种方式使用 Hibernate:

- session 工厂

private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

// Create the SessionFactory from hibernate.cfg.xml
return configuration
.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
//reabrimos la sesion si esta cerrada al liberar los recursos
if(sessionFactory.isClosed())
{
System.out.println("Reopen session");
sessionFactory.openSession();
}

return sessionFactory;
}

然后在我的 hibernate.cfg.xml 中我有以下行:

<property name="current_session_context_class">thread</property>

最后,在我的端点中,我定义了一个 hibernate_session 类,我使用它的方式如下:

@Path("/projects")
public class ProjectServiceImpl {

@Context
SecurityContext security;
Session hibernate_session = null;

@POST
@Path("sync.json")
@Produces(value = {"application/json",
"application/vnd.myapp-v1+json",
"application/vnd.myapp-v2+json"})
public Response syncProjects(
@DefaultValue("") @FormParam("projects") String in_projects_str,
@DefaultValue("0") @FormParam("last_sync") long last_sync,
@Context Request request) {

//...

hibernate_session = HibernateUtil.getSessionFactory()
.getCurrentSession();

if (hibernate_session == null) {
ResponseMessage rm = new ResponseMessage();
rm.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
rm.setMessage("Hibernate Session is Null");
rm.setType("ERROR");
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(rm)
.type("application/json").build();
}

try {

hibernate_session.beginTransaction();

//Database work...

hibernate_session.flush();

hibernate_session.getTransaction().commit();

}catch (RuntimeException | IllegalAccessException
| InvocationTargetException e) {
try {
if (hibernate_session.getTransaction() != null) {
hibernate_session.getTransaction().rollback();
}
} catch (RuntimeException rbe) {
System.err.println("Couldn’t roll back transaction");
}

e.printStackTrace();
ResponseMessage rm = new ResponseMessage();
rm.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
rm.setMessage(e.getMessage());
rm.setType("ERROR");
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(rm)
.type("application/json").build();

}
}

return Response.ok().entity(result_entity)
.type("application/json").build();
}

我的 hibernate_session 是一个类属性,我是否必须将其更改为局部变量?据我所知,端点将在不同的线程中执行,因此我假设我正在使用端点容器类的不同实例,并且这些类属性不会被多个请求覆盖。

您对这个主题的任何见解都将受到赞赏,

提前致谢

最佳答案

谢谢大家的回复。我终于成功解决了这个问题。

在我的多个条目之一中,有一个开始事务(创建条件所必需的)但未提交。结果是,之前调用过该方法的重用线程将引发嵌套异常。通过提交事务,问题就解决了:)

关于java - Hibernate + Jersey + Jackson 随机获取 “org.hibernate.TransactionException: nested transactions not supported”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21608881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com