Spring + hibernate multi-data source applications

Source: Internet
Author: User

I have two databases, test and test1. Both databases have a table named TEST_ONE.
The configuration of applicationContext. xml is as follows:

// Configure database test
<Bean id = "test"
 
Class = "org. springframework. jdbc. datasource. DriverManagerDataSource">
<Property name = "driverClassName" value = "com. mysql. jdbc. Driver">
</Property>
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/test"> </property>
<Property name = "username" value = "root"> </property>
<Property name = "password" value = "root"> </property>
</Bean>

// Configure database test1
<Beanid = "test1"
Class = "org. springframework. jdbc. datasource. DriverManagerDataSource">
<Property name = "driverClassName" value = "com. mysql. jdbc. Driver">
</Property>
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/test1"> </property>
<Property name = "username" value = "root"> </property>
<Property name = "password" value = "root"> </property>

</Bean>
// Integrate two data sources and specify the data source management class
<Beanid = "dataSource" class = "com. keith. dataSource. DynamicDataSource">
<Propertyname = "targetDataSources">
<Mapkey-type = "java. lang. String">
<Entry key = "test" value-ref = "test"/>
<Entry key = "test1" value-ref = "test1"/>
</Map>
</Property>
// Set the default data source configuration
<Property name = "defaultTargetDataSource" ref = "test"/>

</Bean>
<Bean id = "sessionFactory"
Class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource">
<Ref bean = "dataSource"/>
</Property>
<Propertyname = "hibernateProperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. MySQLDialect
</Prop>
</Props>
</Property>
<Propertyname = "mappingResources">
<List>
<Value> com/keith/vo/TestOne. hbm. xml </value> </list>
</Property>
</Bean>
 

Tool class for data source switching:
Public class DBContextHolder {
// Database test
Public static final StringDB_TEST = "test ";
// Database test1
Public static final StringDB_TEST_1 = "test1 ";
Private static finalThreadLocalCON_LOCAL = new ThreadLocal ();
Public static voidsetDB (String type ){
CON_LOCAL.set (type );
}
Public static StringgetDB (){
Return (String) CON_LOCAL.get ();
}
Public static voidclearDB (){
CON_LOCAL.remove ();
}
}


// This class is used to manage the data source, in the configuration file
Public class DynamicDataSource extendsAbstractRoutingDataSource {
@ Override
ProtectedObject determineCurrentLookupKey (){
// TODOAuto-generated method stub
Return DBContextHolder. getDB ();
}

}


// Compile the test class
Public class Test {
Publicstatic void main (String [] args ){
ApplicationContext context = newClassPathXmlApplicationContext ("applicationContext. xml ");
// Dynamically set the data source
DBContextHolder. setDB (DBContextHolder. DB_TEST_1 );
SessionFactory sf = (SessionFactory) context. getBean ("sessionFactory ");
Session s = sf. openSession ();
Transactiontr = s. getTransaction ();
Tr. begin ();
TestOneone = new TestOne ();
One. setName ("aaa ");
S. save (one );
Tr. commit ();
Sf. close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.