We have a similar situation in NHibernate (.NET) because of our system architecture, which uses a schema (database) for each branch.
In this case, you can only use SessionFactory
, assuming the responsibility of creating the connections.
For this, you should:
1) Create a class inherited from org.hibernate.connection.DriverConnectionProvider
(example in C #):
internal class MeuDbConnectionProvider : DriverConnectionProvider
{
protected override string ConnectionString {
// To-Do: Aqui você deve retornar a String de conexão baseada em uma variável
// 'static' (neste meu caso, chamei de "SessionManager") que provê a base em que vc está conectado no momento
get { return SessionManager.CurrentDatabaseConnection.ConnectionString; }
}
public override System.Data.IDbConnection GetConnection()
{
// To-Do: Criar uma nova conexão MySql baseada na String de Conexao
// da "base corrente"
(...)
return novaConexao;
}
}
2) Set Hibernate to use your ConnectionProvider for connection creation.
Configuration configuration = new Configuration()
.setProperty(Environment.CONNECTION_PROVIDER, "com.my.package.MeuDbConnectionProvider");
From now on, power will be in your hands - using only SessionFactory
.
Comments: