JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 使用Hibernate同時連接兩個資料庫
keanu1003





發文: 49
積分: 0
於 2010-09-14 17:40 user profilesend a private message to usersend email to keanu1003reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請教各位先進:
小弟目前使用spring + hibernate同時連接兩個資料庫,如果只單獨連接一個資料庫可以很快完成初始化動作,但是同時設定兩個資料庫的hibernate資料時,初始化時間就會很長(約莫5分鐘),不知道是否有人跟我有同樣的狀況,或是有先進可以告訴我是什麼原因造成初始化時間這麼長,謝謝
相關設定如下:

applicationContext-hibernate.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    <bean id="dataSourceMysql" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.1.1:3306/TestDB?characterEncoding=UTF-8"/>
        <property name="username" value="un"/>
        <property name="password" value="pw"/>
    </bean>
 
    <bean id="dataSourceMssql" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=TestDB;SelectMethod=cursor"/>
        <property name="username" value="testdb"/>
        <property name="password" value="testdb"/>
    </bean>
 
    <bean id="sessionFactoryMysql" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
        <property name="dataSource" ref="dataSourceMysql"/>
        <property name="mappingResources">
            <list>
                <value>model/Cities.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <bean id="sessionFactoryMssql" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
        <property name="dataSource" ref="dataSourceMssql"/>
        <property name="mappingResources">
            <list>
                <value>model/Cities.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
            </props>
        </property>
    </bean>
 
    <bean id="transactionManagerMysql" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactoryMysql"/>
    </bean>
 
    <bean id="transactionManagerMssql" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactoryMssql"/>
    </bean>


Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Date;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 
import dao.CityDao;
import model.City;
 
public class Main {
 
  public static void main(String[] arg){
    Date date1 = new Date();
    System.out.println("====="+date1.getTime());
    String hibernate = System.getProperty("user.dir")+
      "\\WebContent\\WEB-INF\\springBeansConfig\\applicationContext-hibernate.xml";
    
    String resources = System.getProperty("user.dir")+
      "\\WebContent\\WEB-INF\\springBeansConfig\\applicationContext-resources.xml";
    String service = System.getProperty("user.dir")+
      "\\WebContent\\WEB-INF\\springBeansConfig\\applicationContext-service.xml";
    
    String[] con = {resources, hibernate, service}; 
    
    ApplicationContext context = new FileSystemXmlApplicationContext(con);
    
    CityDao cityDao = (CityDao) context.getBean("CityDao");
    
    CityDao cityDaoMssql = (CityDao) context.getBean("CityDaoMssql");
    
    City city = new City();
    city.setArea("1");
    city.setCode("2");
    city.setName("3");
    city.setState(1);
    
    City city1 = new City();
    city1.setArea("1");
    city1.setCode("2");
    city1.setName("3");
    city1.setState(1);
    
    cityDao.saveCity(city);
    cityDaoMssql.saveCity(city1);
    
    cityDao.removeCity(1);
    
    Date date2 = new Date();
    System.out.println("====="+date2.getTime());
    System.out.println("====="+((date2.getTime() - date1.getTime())/1000));
  }
 
}
 


keanu1003 edited on 2010-09-14 17:55
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1323 使用Hibernate同時連接兩個資料庫 keanu1003 4562 2010-09-14 17:40
1084 Re:使用Hibernate同時連接兩個資料庫 GuanChih 27 2010-11-04 23:56
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8