How do I connect to multiple databases using JPA Glassfish Java EE? - persistence

I have an application using JSF,EJB,JPA, Glassfish. There are multiple(may be around thousand or more) clients using my app, however each client has a separate database. All the databases have the same schema. I would like to determine which database connection to use at the time when a user logs into the system.
For example client A enters client code, username, and password and logs in, I determine that client A belongs to database A, grab the connection for database A and continue on my merry way.
I am using JPA as my persistence provider. Is it possible to set datasource in persistence.xml at runtime? Is there .java version of persistence.xml available? Is there a better/preferred way to do this? PersistenceUnit name will be same for all the connections.
Thanks

If you know all users in advance then you can create separate persistence unit for every user in persistence.xml and use factory for select Persistence Unit Name for users. Addition of users, require modification in persistence.xml and redeployment of Application.

Related

how to save application users created in jboss in a database or serialize them

I want to migrate the management and application users created in jboss using add-user.bat utility while upgrading the jboss version.
For that i was thinking if it is possible to store the users created in jboss in a database or may be something like serializing them while creating and update the jaas cache of the jboss server at the time of boot up.
Is there any way to export the user list from an existing jboss installation?
So can anyone please help me with either of the above?
Or may you please suggest me which should be the best approach?
Unfortunately, there is no direct way of exporting application/management users from the JBoss server. But all the application and management users created for jboss will present in application-users.properties and mgmt-users.properties files.
You can get usernames with the encrypted password in the following format
username=HEX( MD5( username ':' realm ':' password))
e.g.
admin=2a0923285184943425d1f53ddd58ec7a
All the roles and groups for the users will present in application-roles.properties
and mgmt-groups.properties files.
Location for above files :
standalone mode: JBOSS_HOME/standalone/configuration/
domain mode: JBOSS_HOME/domain/configuration/

Wildfly won't deploy when datasource is unavailable

I am using wildfly-8.2.0.Final.
There are several databases that i have to connect to. However, some of them are only used for certain functionalities on the web application and they are not needed to be online all the time. So when the wildfly starts, some of the datasources may not be online. However, disconnection to any datasource causes wildfly to not deploy .war deployment and i cannot find any way to solve this problem. Is there a way?
UPDATE:
I have a single table on a remote database server. The user will be able to query the table via my web application. The thing is, I have almost no control over the mentioned database. When the web application starts, it could be offline. However, this would cause my web application to fail to start. What I want is being able to run queries on a remote database if it is online. If it is offline, the web page could fail or the query can be canceled. But the only thing that I don’t want is that my web application to be limited by a remote database that I may have no control over.
My previous solution was a workaround. I would run queries on the remote database via a local database which has a foreign table to the remote one. However, the local one reads all data on the remote table before applying any constraints on postgresql 9.5. As the remote table has a large number of rows and I am using lazy loading, it takes so long for a single query and defeats the whole purpose of the lazy loading.
I found a similar question, but there is no answer.
On wildfly, you can set the datasource so that it tries to reconnect periodically when it disconnects. In my case, the deployment should be successful initially for this to be helpful.
The deployment will failed if it references those datasources.
Also you could define but disable those datasources.

How to store MongoDB database config information in a web application?

While using relational databases like MySQL, we can create a JNDI resource in GlassFish server(which I am using) and then use it as DataSource by looking up the context.
Is there an equivalent way for doing this for MongoDB in GlassFish?
If no then what is the best way to store the connection details like DB username and password for MongoDB without hard-coding them into the application or having some kind of properties files inside the web application from where we access the connections details?
I am using a Java Servlet as my web technology.
You can define an alias in glassfish for your password and access it by calling System.getProperty("mypassword")

How do you configure jndi Glassfish 4 connection to a mongoDB database

I am developing a Jee7 project and would like to employ mongoDB as the backend database.
My jee7 application will run on Glassfish 4.
I wish to use the Glassfish 4 admin console to configure my mongoDB jdbc connection pool etc..
However Glassfish 4 doesn't list mongoDB in its list of supported Database Driver Vendor
Does this mean that you do not configure mongoDB in the same way as say DB2 or MySQL?
I could configure mongoDB using a EJB singleton, but that doesn't feel correct.
I don't think you can without writing your own resource adapter. First, mongodb is non-transactionable, so it's not like it needs to participate in any transaction related events. Second, their java driver manages the connection with their own internal connection pool.
Although it would be nice to configure the resource outside of the app, in reality you should just create a singleton bean and do everything from there.
Also take a look at producers.

Multi tenant MVC4 application and automatic DB migrations with Entity Framework - Good practice?

I've built a multi tenant MVC4 application that use a specific database depending on the hostname.
The site binds to all of our customers hostnames:
If a visitor surfs to 'domain1.com', the 'domain1.com' database is used.
If a visitor surfs to 'domain2.com, the 'domain2.com' database is used.
Automatic migrations are normally placed in Application_Start():
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyProject.Models.MyProjectContext, MyProject.Migrations.Configuration>());
This will run the migrations on application start. However, since my application responds to several hostnames, only one database will be migrated. When i switch to another hostname, that database IS NOT migrated since the application is already loaded into memory on the server.
I solved this by moving the above line of code to Session_Start().
Is this good practice? Is there a better solution?
Thanks in advance,
Andreas