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 7731677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:32:33+00:00 2026-06-01T06:32:33+00:00

Traditionally, we try to avoid LazyInitializationException s. However, I need to temporarily allow them

  • 0

Traditionally, we try to avoid LazyInitializationExceptions. However, I need to temporarily allow them to be thrown. This is the pseudo code of what I’m trying to do:

Session session = ...;

Customer customer = (Customer) session.get(Customer.class, 56);

// All eagerly fetched data is now in memory.

disconnectSession(session);

// Attempts to access lazy data throw LazyInitializationExceptions.
// I want to take advantage of this fact to *only* access data that 
// is currently in memory.

magicallySerializeDataCurrentlyInMemory(customer);

// Now that the in-memory data has been serialized, I want to re-connect
// the session.

reconnectSession(session);

The magicallySerializeDataCurrentlyInMemory method recursively attempts to serialize in-memory data of customer and its related entities, absorbing LazyInitializationExceptions along the way.

Attempt #1: session.disconnect / session.reconnect

In this attempt, I used this pattern:

Connection connection = session.disconnect();

magicallySerializeDataCurrentlyInMemory(customer);

session.reconnect(connection);

Unfortunately, it didn’t throw LazyInitializationExceptions.

Attempt #2: session.close / session.reconnect

In this attempt, I used this pattern:

Connection connection = session.close();

magicallySerializeDataCurrentlyInMemory(customer);

session.reconnect(connection);

Unfortunately, this rendered the session useless after session.reconnect(connection).

How can I temporarily force LazyInitializationExceptions?

  • 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-06-01T06:32:34+00:00Added an answer on June 1, 2026 at 6:32 am

    There isn’t a way to temporarily close the session. What you can do is remove the Entity from the session and then put it back.

    session.evict(customer);
    //do something will throw lazy load
    session.refresh(customer);
    

    connect and reconnect just manually manage what particular JDBC connection is in use. A hibernate Session is a larger concept than a single JDBC connection. The connection is just a resource to it. It can go through using many different physical database connections throughout its lifecycle and not care one bit. If you disconnect then ask the Session to do something, it will just go get another connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a mental block today, need a hand verifying my logic isn't fubar'ed. Traditionally
I tested this block of code and find that the GetInts method does not
I traditionally deploy a set of web pages which allow for manual validation of
When I try to run this query in Access through the ODBC interface into
I recently heard about nedtries and decided to try implementing them, but something bothers
I try to compare Mnesia with more traditional databases. As I understand it tables
Traditionally when using a DbCommand when retrieving data from a sproc, something like the
I'm traditionally a .NET developer and am spoiled for choice when it comes to
I have a pretty common layout issue that I have traditionally used a table
Basically what I mean is like this: List<String[]> routes = (List<String[]>)application.getAttribute(routes); For the above

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.