Connect NHibernate to different databases with same schema - nhibernate

We are in the process of splitting our db into several smaller ones. The schemas will be exactly the same and we will control which db the system connects to when the client logs in. I receive an error if I do not set a connection string in my nhibernate configuration. I do not want to create a factory for each db. Is it possible to have a session factory provide a Session that I can set the connection string before using it?

Have not used it but there is a method ChangedDatabase on the Session.Connection. Maybe that would work?

Maybe you can use NHibernate.Shards, in the NHcontrib repository

Related

Same database - different output from different clients

I have a docker container with Oracle DB which I try to access from different clients with same credentials and its behaviour is quite strange.
I have set up connection in IDEA with following JDBC URL: jdbc:oracle:thin:#//localhost:1526/XEPDB1
Also I work with it via Spring Data framework in another container, url is jdbc:oracle:thin:#oracle:1521/XEPDB1 (port is forwarded to outer world as 1526, container name is oracle)
And finally, I tried to access the db using sqlplus from within db host: sqlplus LOGIN/PASSWORD#localhost:1521/XEPDB1
All connections are successful, but simple inserts and selects for same table show that each client sees only it's own modifications. What is wrong? How do I share same table between clients?
According to the connection strings, all the users are connected to the same database, so that isn't the issue.
The issue is probably that each user that modifies the data (e.g., inserts new rows), does so without committing, and thus this data is only available in their current session. Once they commit the data, it will be available to all the other sessions too.

Dapper changing connection string at runtime

I have three identical database servers (DDL wise) that my application connects to.
I want let application users decide which database they want to connect to.
This is a ASP.NET Core 2.1 API project, which implements both service and repository patterns (database queries are called using Dapper, but this can be changed).
I could simply put server name as a parameter in my Domain project, but I don't want this to know anything about database servers we have, neither I want my Repository to know that there could are multiple servers.
So it's just application side that can decide which database server to connect to at run-time.
I was able to find samples and suggestions for EF Core, but not much for anything else, particularly Dapper.
So my question is. How would I let users change database server at run time while keeping domain and repository unaware that multiple servers are there?
So in this case I would suggest having your connection strings either stored in environment variables or appsettings.json files. Lets say the user wants to access "server1" , this would come in from your api and you could then get out the relevant connection string in your repository layer like so
var connectionString = Environment.GetEnvironmentVariable(serverToUse);
Where serverToUse is "server1" passed in from the api. I prefer storing my connection strings in environment variables as its stored directly on the host machine, instead of the application. You could also do this via appsettings
"Server1" : "Your connection string here" in your appsettings.json
You could also do this in your service layer and have the service layer pass the connection string to the repository . Pick your flavor. In this case the repositories are not concerned with what connection you are using as long as its present in either environment variable or appsettings.

Connecting to specific Azure Redis database in asp .NET core

I want to connect to a specific database in our new Azure Redis cache but can't seem to figure out how to do it.
I've tried adding the database id to connection string in various forms as well as looking for GetDatabase(dbid) on the IDistributedcache object (which doesn't seem to exist).
FYI, I want to use the same cache for our testing and production without having to pay for an additional redis cache so I'm open to alternative approaches.
You could also use the ConnectionMultiplexer object directly and access the Database via the GetDatabase method.

SQL connection pooling in Azure Functions

In traditional webservers you would have a SQL connection pool and persistent connection to the database.
But I am thinking of creating my entire application as Azure Functions.
Will the functions create a new connection the SQL server everytime its called upon?
Azure Functions doesn't currently have SQL as an option for an input or output binding, so you'd need to use the SqlClient classes directly to make your connections and issue your queries.
As long as you follow best practices of disposing your SQL connections (see this for example: C# SQLConnection pooling), you should get pooling by default.
Here's a full example of inserting records into SQL from a function: https://www.codeproject.com/articles/1110663/azure-functions-tutorial-sql-database
Although this is already answered, I believe this answer can provide more information.
If you are not using connection pool then probably you are creating connection every time function is invoked. Creating connection has associated cost, for warmed up instances it is recommended to use connection pool. max number of connection should also be chosen cautiously since there can be couple of parallel functions app running (as per plan).
This is example of connection pool.

Handling Multiple databases with NHibernate in a single application

At the moment I define the connection properties in a configuration file and only ever connect to one database. I'd like to be able to at some point have a user login, figure out (via a seperate central database maybe) what database they should be connected and from that point on all sessions created will talk to that database.
Whats the best way to achieve this. Create a configuration file for every possible database? Or could I have a single session manager and change the connection url on the fly accordingly? What sort of options do i have?
Update: Apologies I should have mentioned this was NHibernate. I didn't think it would matter but some things like Hibernate Shards will not be applicable to be as I believe NHibernate Shards is waiting.
You just need to make two datasources then call the one you need for the specific query.
Please take a look at this:
https://www.hibernate.org/450.html
Some official solutions.
And here:
http://www.java-forums.org/database/159-hibernate-multiple-database.html
an online thread about this issue.