posts - 189,comments - 115,trackbacks - 0
          數據遷移(從mysql3.1遷移到oralce10G)
          ssuupv 發表于 2006-3-2 15:29:00

          最近做了一項目數據移植工作:根據客戶的需求。把原先,mysql3.1平臺的數據遷移到oracle10g:

           剛開始我想通過oralce提供的oracle migration workbench,可是使用中碰到一些問題,一直沒搞出來,在網上也查找不到解決的方案,具體的問題我也不多說了。

           在用戶催了N次的時候,我忽然來了個靈感通過程序的方式,原理,1,從mysql一張表取出所有數據,2,存放到oracle10g 目標數據表里,3.重復執行1,2步驟,直到所有表導入。

          接下來我講一下,我的解決過程。

          我們開發的平臺是用webwork+spring+hibernate。

          1。配置二個hibernate.配置文件1.hibernate_for_mysql.cfg.xml hibernate_for_oracle.cfg.xml

          .hibernate_for_oracle.cfg.xm部分配置:

          ? <property name="hibernate.connection.url"><![CDATA[
          ??????????? jdbc:oracle:thin:@localhost:1521:orcl
          ??????? ]]></property>
          ??????? <!-- Database JDBC driver-->
          ??????? <property name="hibernate.connection.driver_class">
          ??????????? oracle.jdbc.driver.OracleDriver
          ??????? </property>

          hibernate_for_oracle.cfg.xm部分配置:

          ??????? <property name="hibernate.connection.url"><![CDATA[
          ??????????? jdbc:mysql://localhost/flydonkey?useUnicode=true&characterEncoding=gbk
          ??????? ></property>
          ??????? <!-- Database JDBC driver-->
          ??????? <property name="hibernate.connection.driver_class">
          ??????????? org.gjt.mm.mysql.Driver
          ??????? </property>

          2.在spring.xml配置bean主要負責數據遷移的

          ?<bean id="sessionFactory_for_mysql" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" singleton="true">
          ??<property name="configLocation">
          ???<value>classpath:hibernate_for_mysql.cfg.xml</value>
          ??</property>
          ?</bean>
          ?
          ?<bean id="sessionFactory_for_oracle" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" singleton="true">
          ??<property name="configLocation">
          ???<value>classpath:hibernate_for_oracle.cfg.xml</value>
          ??</property>
          ?</bean>

          ?<bean id="getDataFromMysql" class="com.migration.GetDataFromMysqlImpl" singleton="true">
          ?<property name="sessionFactory">
          ??<ref bean="sessionFactory_for_mysql"/>
          ?</property>
          ?</bean>
          ?
          ?? <bean id="insertDataToOracle" class="com.migration.InsertDataToOracleImpl" singleton="true">
          ?<property name="sessionFactory">
          ??<ref bean="sessionFactory_for_oracle"/>
          ?</property>

          編寫bean文件,

          GetDataFromMysqlImpl代碼

          public class GetDataFromMysqlImpl extends EntityDaoImpl implements GetDataFromMysql {

          public List getData(String tableName) {
          ??????? // TODO Auto-generated method stub
          ????? Session session = null;
          ????? List result = null;
          ????? try{
          ????????? session = getSession(true);
          ???????? String strSql = "from "+tableName;
          ???????? result = session.createQuery(strSql).list();?
          ????? }catch(Exception e){
          ????????? e.printStackTrace();
          ????? }finally{
          ????????? try{
          ????????????? if(session!=null){
          ??????????????? session.close();
          ????????????? }
          ??????????? }catch(Exception e){
          ??????????????? e.printStackTrace();
          ??????????? }
          ????????? }
          ????? return result;
          ??? }
          }

          InsertDataToOracleImpl代碼

          public class GetDataFromMysqlImpl extends EntityDaoImpl implements GetDataFromMysql {???

          public void insertData(){

          ?? this.insertTabelName()

          }

          ?private void insertTableName() {
          ??????? Session session = null;
          ??????? Transaction tx = null;
          ??????? try {
          ??????????? session = getSession(true);
          ??????????? GetDataFromMysql gdfm = (GetDataFromMysql) ServiceLocator.getInstance().getService("getDataFromMysql");
          ??????????? List temp = gdfm.getData("tableName");
          ??????????? tx = session.beginTransaction();
          ??????????? if (temp != null) {
          ??????????????? Iterator it = temp.iterator();
          ??????????????? while (it.hasNext()) {
          ??????????????????? TableName?tableName = (TableName) it.next();
          ??????????????????? session.save(tabeName)
          ??????????????? }
          ??????????? }
          ??????????? tx.commit();
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try {
          ??????????????? if (session != null) {
          ??????????????????? session.close();
          ??????????????? }
          ??????????? } catch (Exception e) {
          ??????????????? e.printStackTrace();
          ??????????? }
          ??????? }
          ??? }

          ??? }

          3.隨便寫個客戶端調用些方法。

          4編譯打包

          5.關閉oracle10g所有constraint

          6.運行程序

          100萬紀錄估計30min能跑完,根據各個機器配置高低,會有些差別。

          posted on 2006-03-31 13:10 MEYE 閱讀(520) 評論(0)  編輯  收藏 所屬分類: Study
          主站蜘蛛池模板: 泌阳县| 景洪市| 玛沁县| 汝阳县| 革吉县| 玉龙| 白朗县| 米易县| 三穗县| 汝城县| 灵璧县| 香港| 阜平县| 墨玉县| 聂荣县| 建阳市| 千阳县| 泗洪县| 萨迦县| 长乐市| 马龙县| 浙江省| 邵阳县| 林西县| 龙山县| 禄丰县| 晋中市| 聊城市| 资中县| 大英县| 色达县| 五华县| 剑河县| 揭西县| 获嘉县| 宿松县| 北辰区| 同心县| 昌乐县| 呼和浩特市| 财经|