简体   繁体   English

使用 Hibernate 异步执行存储过程

[英]Executing a stored procedure asynchronously using Hibernate

Is it possible to call a stored procedure asynchronously using Hibernate?是否可以使用 Hibernate 异步调用存储过程? The used connection should be released after triggering the stored procedure while the query continues to run in the background on PostgreSQL.使用的连接应该在触发存储过程后释放,同时查询继续在 PostgreSQL 的后台运行。 The stored procedure should then write a result in a dedicated table from which we can collect it later.然后,存储过程应该将结果写入专用表中,我们稍后可以从中收集它。 Main motive is to prevent connection exhaustion of c3p0 connection pool.主要目的是防止 c3p0 连接池的连接耗尽。

I'm not sure you can do that with Hibernate.我不确定你能不能用 Hibernate 做到这一点。 If you call the procedure the flow control from your program will pause until it finishes.如果您调用该过程,程序中的流程控制将暂停,直到完成。 However you could try to make the stored procedure async.但是,您可以尝试使存储过程异步。 That way when you make the call with hibernate, it will ask the DB to start an async job and your connection will be immediately released.这样,当您使用 hibernate 进行调用时,它会要求数据库启动异步作业,并且您的连接将立即释放。 You'll have to make also some queries to know if your stored procedure has finished.您还必须进行一些查询以了解您的存储过程是否已完成。

There is no support within JDBC for asynchronous execution, so Hibernate does not offer such a feature either. JDBC 中不支持异步执行,因此 Hibernate 也不提供这样的功能。 If you want to return directly from a long running stored procedure call, you want to make this call in a separate thread, maybe even using a ThreadPoolExecutor .如果您想直接从长时间运行的存储过程调用返回,您希望在单独的线程中进行此调用,甚至可能使用ThreadPoolExecutor This will let you proceed with the thread which originally triggered the call.这将让您继续处理最初触发调用的线程。 In this case you can most likely also process the result of the stored procedure in the executing thread, instead of writing into a temp table and then reading from there.在这种情况下,您很可能还可以在执行线程中处理存储过程的结果,而不是写入临时表然后从那里读取。 However, this will depend on your usecase.但是,这将取决于您的用例。 You will, however, need a connection for that, so it won't help you with running out of connections in the pool.但是,您将需要一个连接,因此它不会帮助您解决池中的连接不足问题。 Is this an actual problem?这是一个实际问题吗? Have you configured the c3po pool correctly as well as the underlying database?您是否正确配置了 c3po 池以及底层数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM