Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:49:32+00:00 2026-05-26T17:49:32+00:00

we are developing an (JavaSE-) application which communicates to many clients via persistent tcp-connections.

  • 0

we are developing an (JavaSE-) application which communicates to many clients via persistent tcp-connections. The client connects, performs some/many operations (which are updated to a SQL-Database) and closes the application / disconnects from server. We’re using Hibernate-JPA and manage the EntityManager-lifecycle on our own, using a ThreadLocal-variable. Actually we create a new EntityManager-instance on every client-request which works fine so far. Recently we profiled a bit and we found out that hibernate performs a SELECT-query to the DB before every UPDATE-statement. That is because our entities are in detached-state and every new EntityManager attaches the entity to the persistence context first. This leads to a massive SQL-overhead when the server is under load (because we have an write-heavy application)and we try to eliminate that leak.

  • First, we thought about 2nd-Level-Cache. However, we discovered that hibernate invalidates it’s Query- and Collection-Caches whenever a new item is added or removed.
  • On second thought, we evaluate whether to keep an EntityManager up as long as the client is logged in on the server. But I wonder if this is a “best practice”, because there are some drawbacks: thread-safety, managing-overhead of the EntityManager-instances, etc.

In short: we are looking for a way to get rid of those SELECT-statements before every UPDATE. Any ideas out there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:49:33+00:00Added an answer on May 26, 2026 at 5:49 pm

    One possible way to get rid of select statements when reattaching detached entities is to use Hibernate-specific update() operation instead of merge().

    update() unconditionally runs an update SQL statement and makes detached object persistent. If persistent object with the same identifier already exists in a session, it throws an exception. Thus, it’s a good choice when you are sure that:

    1. Detached object contains modified state that should be saved in the database
    2. Saving that state is the main goal of opening a session for that request (i.e. there were no other operations that loaded entity with the same id in that session)

    In JPA 2.0 you can access Hibernate-specific operations as follows:

    em.unwrap(Session.class).update(o);
    

    See also:

    • 11.6. Modifying detached objects
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing JavaME LWUIT application which needs to display some data on map.
We are developing an application which takes audio from the microphone and does some
When developing a new web based application which version of html should you aim
After developing in VS.NET for many years, I'm starting to do some Xcode development
Developing a .NET WinForms application: how can I check if the window is in
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
Developing a project of mine I realize I have a need for some level
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
When developing a desktop application in .NET, is it possible to not require the
When developing whether its Web or Desktop at which point should a developer switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.