doufutao4428 2012-01-19 17:35
浏览 22

推动ORM并发连接

I am working on a project using Propel ORM and need to connect to two different databases to retrieve certain data. I've attempted searching online for a guide, but had no luck as all the "solutions" were either specific to Symphony or just simply did not work.

I would like to be able to have two connections specified and be able to switch between them during certain queries by passing a connection alias to the query. However, it is also acceptable to have both connections open simultaneously.

Any and all help is greatly appreciated!

Thanks, Dima

  • 写回答

1条回答 默认 最新

  • duangu9173 2012-01-19 17:45
    关注

    According to the Propel API Docs you can define a database connection in your schema.xml file in such a manner:

    <?xml version="1.0" encoding="UTF-8"?>
    <database name="bookstore" defaultIdMethod="native">
        <!-- table definitions go here -->
    </database>
    

    Then in your runtime-conf.xml settings you can set the connection params:

    <datasources default="bookstore">
      <datasource id="bookstore">
        <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
        <connection>
          <dsn>mysql:host=localhost;dbname=my_db_name</dsn>
          <user>my_db_user</user>
          <password>my_db_password</password>
        </connection>
      </datasource>
    </datasources>
    

    -- Edit --

    Doesn't appear that Propel supports an alias attribute for the database tag, but you should be able to get away with the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <database name="bookstore_primary" defaultIdMethod="native">
        <!-- table definitions go here -->
    </database>
    
    <database name="bookstore_secondary" defaultIdMethod="native">
        <!-- table definitions go here -->
    </database>
    
    <datasources default="bookstore_primary">
      <datasource id="bookstore_primary">
        <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
        <connection>
          <dsn>mysql:host=localhost;dbname=bookstore_primary</dsn>
          <user>my_db_user</user>
          <password>my_db_password</password>
        </connection>
      </datasource>
    </datasources>
    
    <datasources default="bookstore_secondary">
      <datasource id="bookstore_secondary">
        <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
        <connection>
          <dsn>mysql:host=localhost;dbname=bookstore_secondary</dsn>
          <user>my_db_user</user>
          <password>my_db_password</password>
        </connection>
      </datasource>
    </datasources>
    

    See if that works. At the end of the day the thing that really matters is that a connection can be made to both databases (using respective DSNs).

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类