How to use Multitenant in Nhibernate with Spring in MVC - spring

I have an application in MVC with Hnibernate deployed on a server. Currently only one client is using this application. Now there are many clients and all will use this app with different database but the schema will same for all.
For this implementation I am thinking an approach-
I have made a new database in which table hold the information regarding the individual Client database connection strings.
When the application run, Nhiberate makes multiple session factories for all database which includes all client databases and the main database.
For example- there are two clients 'A' and 'B' with their database name 'A_db' and 'B_db'. And the other main database which hold connection strings as 'All_db'. Then in this case nhibernate make 3 session factories for all three db.
So when user enter their login credential, i'll check the related connection string for that client from the main database. and then destroy all session factories which are not related to that client database connection string. So by doing this there will be only one session factory remain that belong to his database.
Is this my approach is correct??
And i am going in right direction then provide some code for this approach as making multiple session factories and after then removing all session factories except the related one?

You can supply a connectionstring to the GetSession method. Check out this link for more intel.

Related

Is there a way to create database and seed data in asp.net core 2 when changing the connection string

Is there a way to create the database and seed data in asp.net core 2 when changing the connection string through the OnConfiguring method of the DbContext?
I have designed my app for multi-tenancy (multi-database model) and should be able to register tenants dynamically each with connection string. Now my problem is, how can I create the database and seed data dynamically without restarting the app?
OnConfiguring screenshot
Basically the process of provisioning a database for a new tenant would require some time. Hence, you could follow the below steps.
Create a new tenant in the code
Post a message to the service bus with the necessary information like the tenanid / name etc.
Have a job ( typically the web Job) listen to the messages and run restore a new database from a master dacpac or script. Finally rename the database to the tenant.
Push back a message via service bus to let the application know about the database.
On receipt of the above message, the application updates the connection details for the tenant in the database.
In the other option, you can take a look at the Azure shard map for the tenant database sharding.
HTH

Passing client userid to Oracle when using a pooled connection

Our company has an audit requirement to track individual user interaction with the application by user id. We use Tomcat 7 with the Tomcat connection pool and an Oracle 11.2 database (soon to be 12c). We connect using a Type 4 datasource managed by JNDI on the server which uses a system user id. Users log on using SSO to the web application. We want to make sure that whenever a user modifies a database record, their SSO signon id instead of the system id is used in an audit column to identify who made the modification.
My research shows that using getClientInfo and setClientInfo may be the way to go and I want to be clear on how I would implement this. Would I use the setClientInfo on the pooled datasource Connection to set the client user id, or run the Oracle stored procedure DBMS_APPLICATION_INFO.SET_CLIENT_INFO to pass the client user id? I know that in the Oracle virtual table v$session there is a column for OSUSER that holds this information. How do I get it there? Could I make a direct update to that column?
Since this is a multi-threaded application server, I don't want to end up with a concurrancy issue.

Passing ClientInfo/ClientIdentifier on syscontext/connection with Hibernate for audit purposes

I have a web service which processes inserts/updated data to DB. When client calls this webservice, UserId(currently logged in user to portal) will be sent in Request. I need to pass this userId to Db connection or set it in sys context for Audit purpose. we have existing audit tables and triggers to inserts/updates to Audit table after insert/update on actual table. So to track these changes I need to pass this UserId somehow to connection so that it can be retrieved from DB from Sys Context or $session and inserts in Audit table. I am currently using Spring and Hibernate transactions to process data with DB.
I tried to Set client info on Connection but it's not working. I tried below:
Session session=sessionFactory.getCurrentSession();
SessionImpl sImpl=(SessionImpl) session;
Connection connection=sImpl.connection();
connection.setClientInfo("ClientUser", "ABC");
And also I am trying to set client info by calling Stored procedure DBMS_APPLICATION_INFO.SET_CLIENT_INFO before performing operation on DB every time from application code.but I am not sue if it's a correct way to handle it.
I am trying it with both OCI and thin JDBC drivers but not able find a way to set this user id.
Can someone let me know if there is any efficient way to pass user id on sys context or with Connection. I am currently using hibernate4, Spring, Websphere Server, Oracle DB.
I am using Spring #Transactional to handle hibernate Connections and transactions to perform operation on DB.Connections are from Connection pool and I am using org.springframework.jndi.JndiObjectFactoryBean for dataSource.
is there any way to have interceptor or wrapper around connection to set it when we get the connection from connection pool.
Has anyone done this before?
This is described in spring data JDBC Extensions for the Oracle Database
Chapter
8.2 Configuration of a Custom DataSource Connection Preparer
...but you could implement a ConnectionPreparer that would use the current users login id. That way you can capture user login information even if your data source is configured with a shared user name.
This is a solution for oracle, which I think you are using. It should be also possible to adapt that to another database.

Confusion in Oracle naming

I wanted to form oracle database URL for my JDBC conection and few websites say SID should be part of URL and few say schema name.
Please compare schema, service name and SID in ORACLE? And how they are related?
is it like SID:schema is 1:n as in one SID can point to n schemas?
Please explain with the relation between them.
A SID is the unique identifier for a database, or if it's a RAC system for an instance of a database.
A Service is an identifier for a service offered by the database, and the database should be configured for services such as "BILLING_APP" or "CUST_WEBSITE". http://docs.oracle.com/cd/B28359_01/server.111/b28320/initparams217.htm
Don't confuse a database and an instance, by the way. A database is the set of data and control (etc) files, the instance is the memory areas and processes that access it. RAC has multiple instances per database, each of which can register with the listener to offer all or a subset of the total services offered by the database.
A schema is effectively the username that owns the objects, and there can be multiple schemas in the database.
So an application should be referencing a SERVICE to connect to, through a listener on a certain host and port. The listener resolves this to a database (possibly one of many that register to offer that service). The connection is made to the database as a user who might or might not be the owner of the schema. Typically the connection is not made as the schema owner for security reasons -- in fact the best level of security is to connect as User_A, calling code in User_B's schema, whicxh references tables in other users' schemas.
A SID is a Service ID and refers to a single Oracle database instance
A service name is very like a SID but multiple SIDs can be referenced by one Service Name
E.g. in a Dataguard scenario, the primary instance might be SID_01 and ORA_SRV. SID_02 and SID_03 are running somewhere on the network and receiving and applying the log files. SID_01 fails and SID_02 is brought up (Mounted and Opened) and now exposes itself ont he network as ORA_SRV.
Similarly with RAC multiple SIDs make up a single Service.
A schema is in effect a user.
A JDBC connection will be in some form of "HOST:PORT:(SID or Service name)". You will have to connect to the DB and will do so with a username and password. The username will (almost always) connect you to a schema of that name. You can can reference objects in another schema with dotted notation in your sql, e.g. SCHEMA.OBJECT_NAME

Spring Datasource and Database Schema

I am trying to declare a Spring datasource pointing to a DB2 database. Presently I am using a org.springframework.jdbc.datasource.DriverManagerDataSource to setup the connection but am not finding any way to specify the database schema in the database in the datasource bean. Could anyone help me on this?
Problem is there is no standard way to set the schema, each database has a different mechanism.
A work around is to set the schema as part of the db url...
For db2 the url will look something like:
jdbc:db2://SERVER_NAME:PORT/DATABASE:currentSchema=SCHEMA_NAME;
hope that helps...
Special note: make sure you add the semicolon ; at the end of the URL, otherwise you will get errors saying URL is invalid. Also make sure nothing after last ; exists (not even spaces).
There isn't a means to do this with the standard Spring namespace. Rob Harrop's response to a request to add the schema to the configuration:
In general, this kind of functionality should be pushed into the connection pool since there is no really elegant and performant way to do this via a decorator. The pool can set the schema once per connection it creates, whereas here you have to set it each time a connection is retrieved.
If you're desperate to set the proxy in your configuration, the submitter included some code for a proxy to allow the schema to be specified.
If your connection uses the owner of the schema as the user then that connection will point to that particular schema.
ie. If user user1 is the owner of the database schema named schema1 then if you connect to the database using user user1 then by default the connection will point to schema1.
We have used UserCredentialsDataSourceAdapter provided by spring to connect to different schemas based on the logged in user. This provides a datasource which is pointing to a particular schema based on the user. This uses a thread based login information. Each thread has to decide to which schema it has to connect and provide the user according to that.

Resources