Spring+Hibernate: How to handle Multiply (Unlimited) Data Sources and Session Factories dynamically? -


Our application can have multiplied databases. The same application can be different types of database (Oracle and MS SQL) for example Database structures are slightly different but only one set of POJOs is used in hibernation. The list Audi database can be set only when the application actually starts (so it can not be stored as a certain XML)

The user will choose a DB during the login process is required. The selected DB is deposited in the HTTP session.

So I want to use auto-wing sping for auto-wired session factory for the DAO in Web Controllers depending on the current user. Liked: @Regository @transial @ Controller Public Essentials UserGroupDAO {@Autowired @Qualifier ("sessionFactory") Private session session Factory; Public listing & lt; Users & gt; GetListOfUsers () {session session = session Factory.current session (); Return session.Createreitia (user.class) .list (); }} @Controller Public Class Web Controller {@Autowired @Qualifier ("UserGroup DOA") Private Usgramdio User GroupDo; @RequestMapping ("/ greeting.html") Public Model and Visual Greetings (Model Models) {userGroupDAO.getListOfUsers (); }

For example: 2 DB- Oracle DB and MScQLDB and two users A and B User A has logged in Oracle_db, logged into user b MS_SQL_DB when the user accepts a greeting / greeting, the list of users is visible from Oracle_DB, when user BHit / greeting - from MS_SQL_DB

I reviewed the abstrurting datasheet, but it seems that this single session works only with multiplication for a factory, the source of data.

Problem Summary

The databases are dynamically generated, so the config file Can not have hard code database settings The Spring app should be able to determine which databases are available and then connect to them.

Solution 1) Set the 'session' radius for usergroups, sessionfication, etc.

  @Repository @Transactional @Scope (value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) Public category UserGroupDAO Expansion of GeneralDAO {/ code>  

2) Singleton (Data Source Provider ) Start sessionfactory, session, etc. manually and get it from singleton in ApplicationContextConfig

  @configure @ComponentScan (. ...) @EnableTransactionManagement public class ApplicationContext Go back to {{@Autowired @Bean ("sessionFactory") @ Scope (value = "session") Public Sessions - Factory getSessionFactory (Data Source Data Source) {DataSourceProvider.getInstance (). GetDESSourceByContext (). GetSessionFactory (); } @Atovirer @ Bean ("Datasource") @ Scope (value = "session") Return Public Data Sorod getDataSource () {DataSourceProvider.getInstance (). GetDatasourceByContext (). GetDataSource (); }   

3) Detect current DB within datashrespere

  public class data source provider {Private Static DataSource Provider Format = New DataSource Provider (); Public static data source provider getInstance () {return example; } Private Hashmap & lt; String, MyDatasource & gt; Source of Data; Private DataSource Provider () {} Public MyDataSource getDatasourceByContext () {string connection name; If (RequestContextHolder.getRequestAttributes (for example ServletRequestAttributes) {ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes (); HttpServletRequest request = attributes.getRequest (); If (request == faucet) {connectionName = null; } else {// "currentDatabase" is set during the logon connection name = (string) request.getSession (). GetAttribute ("currentDatabase"); }} And {connectionName = null; } If (connectionName! = Null) {MyDataSource dataSource = DataSources.get (connectionName); If (datasource! = Null) {return data source; }} New IllegalStateException Throw ("no data source for reference"); }    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -