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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:54:53+00:00 2026-05-27T07:54:53+00:00

i need to select an EntityManager in my JavaEE6 project (JBoss7) depending of a

  • 0

i need to select an EntityManager in my JavaEE6 project (JBoss7) depending of a login session.
A user “Peter” will result in using databse dbPeter and a user “Paul” will result in using database dbPaul.

The question is now, how can i produce an entity manager that is automatically bind to the according database/persistence unit?

I use CDI to inject the EntityManager. So my first step is to have a producer for EntityManager that looks for the login information from the session and selects the database.
So, how do i select the database/persistence context in this prducer??
Since Jboss7 i can’t see any jndi entry for the persistence unit anymore, so a jndi lookup can not be considered.

I tried using

@Produces
public EntityManager produceEM() {
    EntityManagerFactory managerFactory = Persistence.createEntityManagerFactory( "dbPaul" );

    return managerFactory.createEntityManager();
}

but this fails during deployment with

HHH000231: Schema export unsuccessful: java.sql.SQLException: You cannot set autocommit during a managed transaction!

Thats ok because the place where the em is injected is mostly inside a transaction.
Anyone an idea?

  • 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-27T07:54:54+00:00Added an answer on May 27, 2026 at 7:54 am

    First off, I would be reluctant to create an EntityManagerFactory on the fly. I suppose you would have to do that in every single DAO instance creation (or whatever EJB object you are using to access your db). If that’s the case, think twice about it: creating an EntityManagerFactory is very expensive and should usually be done once in an app lifecycle. As an alternative I would probably create different DAOs for each persistence unit, moving the responsibility of using one or the other to an upper layer, so that you can still rely on the container for creating the EntityManager.

    That being said, in your context and assuming that the method produceEM() is in an EJB and you are using container managed security, you can inject the EJBContext via:

    @Resource
    EJBContext ejbContext;
    

    From here you can get the Principal and create your EntityManager depending on it.

    If any of the assumptions is not valid, let me know and will try to update my answer.

    EDITED:

    @Stateless
    public class EntityManagerService {
    
       @Resource
       EJBContext ejbContext;
    
       @Produces
       public EntityManager produceEM() {
    
       EntityManagerFactory managerFactory = Persistence.createEntityManagerFactory(ejbContext.getCallerPrincipal().getName());
    
       return managerFactory.createEntityManager();
    
       }
    }
    

    You can also create your own mapping between user names and db names which is probably a good idea if you want some decoupling between those.

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

Sidebar

Related Questions

I need select result from mysql regarding query string. Let's string will be: Z
I am using Listbox for display a list. I need select all above item
I need select's option to be actually selected on document ready depending on an
how can i express in xpath a) need select elements which contains specified keyword.
I need to select data when a page is viewed and update the 'views'
I need to select a bunch of data into a temp table to then
I need a select from table which does not have column that tells when
I need the Select Data Source dialog added to my application so that the
I need to select a datetime column in a table. However, I want the
I need to select the position of a following sibling via XSL. This is

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.