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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:08:55+00:00 2026-06-18T16:08:55+00:00

I have an application using Spring MVC and Spring Security. The application project has

  • 0

I have an application using Spring MVC and Spring Security.

The application project has ejbs and web application.

I have a common database for users and one database by users’ group.

So, when a user authenticate, Spring Security use the common database to check login/password and when a controller calls an ejb, the entity manager connects to the user’s group database.

My goal is : create users/groups and their associated database on the fly.

Example :

The common database localhost:3306 database_schema1 contains

  USERS
----------------------
| NAME    | GROUP_ID |
----------------------
| Pierre  |        1 |
| Paul    |        1 |
| Jacques |        2 |
----------------------

  GROUPS
------------------------------------------
| ID | URL            | SCHEMA_NAME      |
------------------------------------------
|  1 | localhost:3307 | database_schema1 |
|  2 | localhost:3308 | database_schema1 |
------------------------------------------

When Pierre authenticate, the home page call an ejb. This ejb has an entity manager which has to connects to group 1 database localhost:3307 with schema database_schema1

If Jacques authenticate, it must be group 2 database.

How can I do to tell to the entity manager to use a given database that I don’t know when I build the project?

I mean, I don’t want to use persistence.xml in my ejb but dynamically initialize entity manager depending on the user authenticated.

Does Persistence.createEntityManagerFactory() will work in this context ? And if it does, is it the best solution?

  • 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-18T16:08:56+00:00Added an answer on June 18, 2026 at 4:08 pm

    You can do this by removing the database URL from persistence.xml and specifying it at runtime:

    Map<String, String> params = new HashMap<String, String>();
    // put the correct URL here
    // you can also specify any other parameter from 'persistence.xml'
    String dbUrl = "jdbc:mysql://localhost:3308/database_schema1"; 
    params.put("javax.persistence.jdbc.url", dbUrl);
    EntityManagerFactory factory = 
      Persistence.createEntityManagerFactory("...", params);

    Then, use this factory object to create EntityManager instances as needed.

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

Sidebar

Related Questions

I'm creating a web application using spring mvc. I have started to incorporate the
We have a Spring web application created using Spring MVC 3.0 In the same
I'm trying to make this 2-player web game application using Spring MVC. I have
I'm trying to write a web application using Spring MVC. I have a configuration
I done my MVC web application using spring security 2.0.4 and spring 2.5 and
I am integrating security to my MVC application using spring security. My web.xml snippet
I have my application using Spring MVC + Apache 2 tiles + Spring security,
In a web application written using spring-MVC, I want to allow users to change
I am prototyping a web application using Spring MVC 3.0 with JasperReports. I have
I have a web application and im using spring mvc. I need to allow

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.