Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having multiple sessionFactory instances

I am porting a legacy application to hibernate 5 and I'm having trouble with the login phase. Here's how it works (I can't change that):

  • user initially connects to oracle DB with a generic login/password (same for all users)
  • then user runs a "login" stored procedure and enters a unique password as parameter
  • the procedure returns a specific Oracle DB username/password to the user
  • user disconnects from DB and reconnects using the credentials given by the stored procedure

I currently create one instance of sessionFactory per connected user, but I'm worried that this will impact performance. Is there a better way to do this?

Thanks

like image 314
user2711115 Avatar asked Jul 13 '17 07:07

user2711115


People also ask

Can SessionFactory be reused?

hibernate. HibernateException: HHH000469: The ClassLoaderService can not be reused.

How many SessionFactory instance exists for each database?

You would need one SessionFactory object per database using a separate configuration file. So, if you are using multiple databases, then you would have to create multiple SessionFactory objects.

How many instances of session and SessionFactory are usually created?

Sessionfactory will create and manage the sessions. If you have say, 4 datasources/databases, then you must create 4 session factory instances. sessionfactory is an immutable object and it will be created as a singleton while the server initializes itself.

Can we have multiple SessionFactory?

Session factory is long live multithreaded object. Usually one session factory should be created for one database. When you have multiple databases in your application you should create multiple SessionFactory object.


1 Answers

Hibernate Multitenancy with "Separate database" strategy would work even if you are actually connecting to the same database but different credentials. MultiTenantConnectionProvider must be specified to return connection with right username and password.

like image 94
ikettu Avatar answered Oct 03 '22 10:10

ikettu