Recently in a data center project, you need to query multiple databases, currently there are 3 solutions in the project:
1. Create a new Sessionfactory
2. Use MyBatis
3. Use SPRINGJDBC
My function as long as query, do not need a transaction, so I choose the simplest way is to create a new sessionfactory in the configuration file.
<bean id= "SESSIONFACTORYDC" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "DataSource" ref= "Dcenter"/> <property name= "hibernateproperties" > <props& Gt <prop key= "Hibernate.dialect" >${hibernate.dialect}</prop> <prop key= "Hibernate.show_sql" > ${hibernate.show_sql}</prop> <prop key= "Hibernate.format_sql" >${hibernate.format_sql}</prop& Gt <prop key= "Hibernate.hbm2ddl.auto" >${hibernate.hbm2ddl.auto}</prop> <prop key= "Hibernate.gen Erate_statistics ">${hibernate.generate_statistics}</prop> <prop key=" Hibernate.cache.use_second _level_cache ">${hibernate.cache.use_second_level_cache}</prop> <prop key=" hibernate.cache.use_q Uery_cache ">${hibernate.cache.use_query_cache}</prop> <prop key=" Hibernate.cache.Region.factory_class ">${hibernate.cache.region.factory_class}</prop> <prop key=" Net.sf.ehcache. Configurationresourcename ">${net.sf.ehcache.configurationResourceName}</prop> <prop key=" Hibern Ate.jdbc.batch_size ">0</prop> <!--<prop key=" Hibernate.use_nationalized_character_data ">f alse</prop>--> <prop key= "Hibernate.search.default.indexBase" >${hibernate.search.default.ind exbase}</prop> </props> </property> </bean>
Dcenter is a remote data source, and I inject it in the program with annotations of spring @Resource or @[email protected].
@Autowiredpublic void Setsessionfactory (@Qualifier ("<span style=" font-family:arial, Helvetica, Sans-serif; " >sessionfactorydc</span><span style= "font-family:arial, Helvetica, Sans-serif;" > ") </span><span style=" font-family:arial, Helvetica, Sans-serif; " >final sessionfactory sessionfactory) {</span>custdao = new Hibernatedao<map, String> (SessionFactory, Map.class);}
Find injected or original data source. Research, found inside there is a method, manually get the new sessionfactory bean to call Createsqlquery modified into this kind of
@Autowiredpublic void Setsessionfactory (final sessionfactory sessionfactory) {Custdao = new Hibernatedao<map, String > (sessionfactory, Map.class) {@Overridepublic sqlquery createsqlquery (String sql, Parameter Parameter) { Sessionfactory SF = (sessionfactory) springcontextholder.getbean ("SESSIONFACTORYDC"); SQLQuery query = Sf.getcurrentsession (). createsqlquery (SQL); setparameter (query, parameter); return query;}};}
Find or use the original data source, what happened, the study found that the difference between getcurrentsession and opensession ()
1. Opensession can literally be seen, is to open a new session object, and each use is to open a new session, if the continuous use of multiple, then the session is not the same object, And you need to call the Close method to close the session after use.
2. Getcurrentsession, literally can be seen, is to get the current context a session object, when the first time you use this method, will automatically produce a session object, and continuous use of multiple times, The resulting session is the same object, which is one of the differences with opensession, and simply put, getcurrentsession is: if there is already used, use the old, if not, build new.
My class is contained in the original transaction through the declarative transaction, I use Getcurrentsession to fetch the sessionfactory inside the transaction, all always use the original data source. Changed it a bit:
@Autowiredpublic void Setsessionfactory (final sessionfactory sessionfactory) {Custdao = new Hibernatedao<map, String > (sessionfactory, Map.class) {@Overridepublic sqlquery createsqlquery (String sql, Parameter Parameter) { Sessionfactory SF = (sessionfactory) springcontextholder.getbean ("SESSIONFACTORYDC"); SQLQuery query = Sf.opensession (). createsqlquery (SQL); setparameter (query, parameter); return query;}};}
Using Opensession (), we found that the correct data source was obtained.
Because I do not need a transaction for this function, so I use a simple write dead new sessionfactory, you need to do business with the dynamic creation of Sessionfactory method
http://fangang.iteye.com/blog/72486 This is explained in detail.
One of the reasons why spring relies on injection failure