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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:52:09+00:00 2026-05-13T05:52:09+00:00

Using a ClassPathXmlApplicationContext object I want to get the same EntityManager that is being

  • 0

Using a ClassPathXmlApplicationContext object I want to get the same EntityManager that is being used by other parts of the app which get it injected via:

@PersistenceContext(unitName="accessControlDb") private EntityManager em;

Using ctx.getBean(“access-emf”) I can get the EntityManagerFactory which is defined in the applicationContext.xml. Using that I can create a new EntityManager, but I can’t get the existing EntityManager used by the rest of the app.

I just can’t figure out what code is executed to inject the value for the @PersistenceContext annotation.

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>


<bean id="innerNgsdpDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource">
    <property name="driverName" value="${ngsdp.jdbc.driver}"/>
    <property name="url" value="${ngsdp.jdbc.url}"/>
    <property name="user" value="${ngsdp.jdbc.username}"/>
    <property name="password" value="${ngsdp.jdbc.password}"/>
    <property name="transactionManager" ref="jotm"/>
</bean>

<bean id="ngsdpDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
    <property name="transactionManager" ref="jotm"/>
    <property name="dataSource" ref="innerNgsdpDataSource"/>
    <property name="user" value="${ngsdp.jdbc.username}"/>
    <property name="password" value="${ngsdp.jdbc.password}"/>
    <property name="maxSize" value="4"/>

    <property name="checkLevelObject" value="2"/>
    <property name="jdbcTestStmt" value="select 1 from dual"/>
</bean>

<bean id="myEmf" name="moservices" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="ngsdpDataSource"/>
    <property name="persistenceXmlLocation" value="WEB-INF/moservices-persistence.xml" />
    <property name="jpaVendorAdapter" ref="hibernate_jpa_vendor_adapter" />
    <property name="jpaPropertyMap" ref="jpa_property_map"/>
    <property name="jpaDialect" ref="hibernate_jpa_dialect"/>
</bean>
  • 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-13T05:52:09+00:00Added an answer on May 13, 2026 at 5:52 am

    If using spring-managed transactions, you can get the current EntityManager by calling

    EntityManagerFactory emFactory = ctx.getBean("access-emf");
    EntityManagerHolder emHolder = 
            (EntityManagerHolder) TransactionSynchronizationManager.getResource(emFactory);
    EntityManager em = emHolder.getEntityManager();
    

    This is most often the current EntityManager. But this is something which should be avoided (except possibly in unit-tests), as stated in the spring docs:

    To be used by resource management code but not by typical application code

    Another approach might be to intercept your service calls using Spring AOP, inject the @PersistenceContext in the advice, and set in in a ThreadLocal of yours. Later, you can get it from that ThreadLocal.

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

Sidebar

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here: http://roticv.rantx.com/book/Eulerianpathandcircuit.pdf you can read among other things, that it… May 13, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer The inner non-static classes in java receive parent instance (for… May 13, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer You may want to check out the contentEditable property as… May 13, 2026 at 11:44 pm

Related Questions

With Spring I can autowire a bean with the following property: @PersistenceContext(unitName=foo) private EntityManager
I'm trying to run the Camel Example camel-example-spring-jms (also at http://activemq.apache.org/camel/tutorial-jmsremoting.html ). However, when
Using a configuration file I want to enable myself to turn on and off
Using a web service is often an excellent architectural approach. And, with the advent
Using a DB with C++ is a real mess and it was refreshing when

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.