Java – how to use hibernate sessions?

My program structure is as follows:

In my main Java class:

for () // this will execute for say 5000 times ---- LINE 1
{
    // do select on Database1 (this will select say 10000 rows) ---- LINE 2

    // do some computations (mainly string operations) ---- LINE 3

    call function1() // this will do some update on Database1 ---- LINE 4
}

Now, I'm trying to use hibernate to access database1 My question is how I should use hibernate session to access it When should I start and end the meeting?

If I start it before the for () loop, can I pass the same session to function1 () because it accesses the same database? Or once I have made a choice (line 1), must I close it and open a new session in function1()? My main concern is to optimize overall performance and minimize overall execution time

I'm new to hibernate So if I ask a very stupid question, please forgive me

Solution

Assuming that you want all updates to be a single atomic transaction, you need to open the session and start the transaction before the loop Then, after the loop, you will commit the transaction and close the session

If each update should be its own atomic transaction, you should still open only one session and use a new transaction for each loop iteration

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>