Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Switch Connection in Interceptor #3463

Unanswered
coado asked this question in Q&A
Discussion options

I currently have 5 database instances running on Docker and I am attempting to load balance SQL requests by modifying the connection in the interceptor. However, simply replacing the connection string is not a viable solution. I assume that I need to open a new connection to a different database automatically after building the SQL query, but I am not sure how.

I have to use NHibernate for this task with interceptors. I'm curious how to implement this kind of load balancing with NHibernate.

public class Interceptor : EmptyInterceptor
{
 private readonly List<DbInstance> _dBsConnStr = Reader.DBsConnectionStrings;
 
 public override SqlString OnPrepareStatement(SqlString sql)
 {
 
 using var session = NHibernate.Context.CurrentSessionContext.HasBind(NHibernateHelper.SessionFactory)
 ? NHibernateHelper.SessionFactory.GetCurrentSession()
 : NHibernateHelper.SessionFactory.OpenSession();
 
 session.Connection.ConnectionString = _dBsConnStr[1].ConnectionString;
 
 return sql;
 
 }
 
}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #3462 on December 15, 2023 22:12.

AltStyle によって変換されたページ (->オリジナル) /