简体   繁体   中英

Executing a stored procedure asynchronously using Hibernate

Is it possible to call a stored procedure asynchronously using Hibernate? The used connection should be released after triggering the stored procedure while the query continues to run in the background on 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.

I'm not sure you can do that with 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. 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. 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 . 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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