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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:36:03+00:00 2026-06-03T15:36:03+00:00

This is a weird situation and I normally would never do it but our

  • 0

This is a weird situation and I normally would never do it but our system has unfortunately now required this kind of scenario.

The System
We are running a Spring/Hibernate applications that is using OpenSessionInView and TransactionInterceptor to manage our transactions. For the most part it works great. However, we have recently required the need to spawn a number of threads to make some concurrent HTTP requests to providers.

The Problem
We need the entity that is passed into the thread to have all of the data that we have updated in our current transaction. The problem is we spawn the thread deep down in the guts of our service layer and it’s very difficult to make a smaller transaction to allow this work. We tried originally just passing the entity to the thread and just calling:

leadDao.update(lead);

The problem is that we than get the error about the entity living in two sessions. Next we try to commit the original transaction and reopen as soon as the threads are complete.
This is what I have listed here.

try {
        logger.info("------- BEGIN MULTITHREAD PING for leadId:" + lead.getId());
        start = new Date();
        leadDao.commitTransaction();
        List<Future<T>> futures = pool.invokeAll(buyerClientThreads, lead.getAffiliate().getPingTimeout(), TimeUnit.SECONDS);
        for (int i = 0; i < futures.size(); i++) {
            Future<T> future = futures.get(i);
            T leadStatus = null;
            try {
                leadStatus = future.get();
                if (logger.isDebugEnabled())
                    logger.debug("Retrieved results from thread buyer" + leadStatus.getLeadBuyer().getName() + " leadId:" + leadStatus.getLead().getId() + " time:" + DateUtils.formatDate(start, "HH:mm:ss"));
            } catch (CancellationException e) {
                leadStatus = extractErrorPingLeadStatus(lead, "Timeout - CancellationException", buyerClientThreads.get(i).getBuyerClient().getLeadBuyer(), buyerClientThreads.get(i).getBuyerClient().constructPingLeadStatusInstance());
                leadStatus.setTimeout(true);
                leadStatus.setResponseTime(new Date().getTime() - start.getTime());
                logger.debug("We had a ping that didn't make it in time");
            }
            if (leadStatus != null) {
                completed.add(leadStatus);
            }
        }
    } catch (InterruptedException e) {
        logger.debug("There was a problem calling the pool of pings", e);
    } catch (ExecutionException e) {
        logger.error("There was a problem calling the pool of pings", e);
    }
    leadDao.beginNewTransaction();

The begin transaction looks like this:

public void beginNewTransaction() {
    if (getCurrentSession().isConnected()) {
        logger.info("Session is not connected");
        getCurrentSession().reconnect();
        if (getCurrentSession().isConnected()) {
            logger.info("Now connected!");
        } else {
            logger.info("STill not connected---------------");
        }
    } else if (getCurrentSession().isOpen()) {
        logger.info("Session is not open");
    }
    getCurrentSession().beginTransaction();
    logger.info("BEGINNING TRANSAACTION - " + getCurrentSession().getTransaction().isActive());

}

The threads are using TransactionTemplates since my buyerClient object is not managed by spring (long involved requirements).
Here is that code:

@SuppressWarnings("unchecked")
    private T processPing(Lead lead) {
        Date now = new Date();
        if (logger.isDebugEnabled()) {
            logger.debug("BEGIN PINGING BUYER " + getLeadBuyer().getName() + " for leadId:" + lead.getId() + " time:" + DateUtils.formatDate(now, "HH:mm:ss:Z"));
        }
        Object leadStatus = transaction(lead);
        if (logger.isDebugEnabled()) {
            logger.debug("PING COMPLETE FOR BUYER " + getLeadBuyer().getName() + " for leadId:" + lead.getId() + " time:" + DateUtils.formatDate(now, "HH:mm:ss:Z"));
        }
        return (T) leadStatus;
    }

    public T transaction(final Lead incomingLead) {
        final T pingLeadStatus = this.constructPingLeadStatusInstance();
        Lead lead = leadDao.fetchLeadById(incomingLead.getId());    
        T object = transactionTemplate.execute(new TransactionCallback<T>() {

            @Override
            public T doInTransaction(TransactionStatus status) {
                Date startTime = null, endTime = null;

                logger.info("incomingLead obfid:" + incomingLead.getObfuscatedAffiliateId() + " affiliateId:" + incomingLead.getAffiliate().getId());

                T leadStatus = null;
                if (leadStatus == null) {
                    leadStatus = filterLead(incomingLead);
                }
                if (leadStatus == null) {
                    leadStatus = pingLeadStatus;
                    leadStatus.setLead(incomingLead);
...LOTS OF CODE
}
                if (logger.isDebugEnabled())
                    logger.debug("RETURNING LEADSTATUS FOR BUYER " + getLeadBuyer().getName() + " for leadId:" + incomingLead.getId() + " time:" + DateUtils.formatDate(new Date(), "HH:mm:ss:Z"));
                return leadStatus;
            }
        });
        if (logger.isDebugEnabled()) {
            logger.debug("Transaction complete for buyer:" + getLeadBuyer().getName() + " leadId:" + incomingLead.getId() + " time:" + DateUtils.formatDate(new Date(), "HH:mm:ss:Z"));
        }

        return object;
    }

However, when we begin our new transaction we get this error:

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

My Goal
My goal is to be able to have that entity fully initalized on the other side or Does anyone have any ideas on how I can commit the data to the database so the thread can have a fully populated object. Or, have a way to query for a full object?
Thanks I know this is really involved. I apologize if I haven’t been clear enough.

I have tried
Hibernate.initialize()
saveWithFlush()
update(lead)

  • 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-03T15:36:05+00:00Added an answer on June 3, 2026 at 3:36 pm

    Thanks @gkamal for your help.
    For everyone living in posterity. The answer to my dilemma was a left over call to hibernateTemplate instead of getCurrentSession(). I made the move about a year and a half ago and for some reason missed a few key places. This was generating a second transaction. After that I was able to use @gkamal suggestion and evict the object and grab it again.

    This post helped me figure it out:
    http://forum.springsource.org/showthread.php?26782-Illegal-attempt-to-associate-a-collection-with-two-open-sessions

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

Sidebar

Related Questions

I have a weird situation. A client of mine has a website having this
Hi this may seem like a weird question, but here's my situation: I have
Ok, this situation is a little weird but anyway. This PHP code generates several
I'm having this weird situation : My user's and system's PATH variable is different
I have this weird situation where my form doesn't send any data, but the
I am facing a very weird situation on Mac OS X. This has bee
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
I am refreshing openmp a bit, and got into this weird situation. Shaved off
I'm sort of in a weird situation... One of which I've never come into
I have this weird situation. I have these two classes: Public Class Entry End

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.