Pregunta

I have a requirement to do hundreds of inserts in a certain part of a web app. It currently works by looping over a prepared statement and committing each one individually. This process is very prone to a Transaction timeout (Extending the timeout time is not viable). It needs to be completely rewritten to be more efficient. I have been able to come up with two ideas:

  1. Instead of using a prepared statement use a normal statement and simple loop over each additional item I want to insert and append it to the query. I am not very concerned with sql injection because this all stems off of one single id that is verified earlier in the process. Please feel free to tell me if this is naive.

  2. Have a stored procedure in the database that does all of the inserts. I would call it by using a callable statement.

If there is another (better) way not mentioned here, please let me know.

¿Fue útil?

Solución

You can try batch insert instead of simple loop http://viralpatel.net/blogs/batch-insert-in-java-jdbc/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top