S2Daoで複数のデータソースを使う(.Net版)
今構築しているアプリケーションで違うサーバのDBを参照する必要があるような機能があり、Dao毎に違うコネクションを使って複数DBへ接続する設定をメモしておきます。
環境
DBサーバ1:postgres 192.168.0.100 テーブル:Employee DBサーバ2:postgres 192.168.0.200 テーブル:AppUser
diconファイル構成
それぞれのdiconファイルの構成です。
ポイントは、datasourceごとにネームスペースを振ったファイルを作ることです。
app.dicon ├─dao1.dicon │ └─datasource1.dicon │ │ └─dao2.dicon └─datasource2.dicon
diconファイル内容
datasource1.dicon
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="datasource1"> <component name="NpgsqlProvider" class="Seasar.Extension.ADO.DataProvider"> <property name="ConnectionType">"Npgsql.NpgsqlConnection"</property> <property name="CommandType">"Npgsql.NpgsqlCommand"</property> <property name="ParameterType">"Npgsql.NpgsqlParameter"</property> <property name="DataAdapterType">"Npgsql.NpgsqlDataAdapter"</property> </component> <component name="NpgsqlDataSource" class="Seasar.Extension.Tx.Impl.TxDataSource"> <property name="DataProvider">NpgsqlProvider</property> <property name="ConnectionString"> "Server=192.168.0.100;Port=5432;User Id=hoge;Password=hogehoge;Database=hogeDB;Encoding=UTF-8" </property> </component> <component class="Seasar.Extension.ADO.Impl.BasicDataReaderFactory" /> <component class="Seasar.Extension.ADO.Impl.BasicCommandFactory" /> <component class="Seasar.Dao.Impl.FieldAnnotationReaderFactory" /> <component class="Seasar.Dao.Impl.DaoMetaDataFactoryImpl" /> <component name="DaoInterceptor" class="Seasar.Dao.Interceptors.S2DaoInterceptor"/> <component name="LocalRequiredTx" class="Seasar.Extension.Tx.TransactionInterceptor"> <arg> <component class="Seasar.Extension.Tx.Impl.LocalRequiredTxHandler" /> </arg> <property name="TransactionStateHandler">TransactionContext</property> </component> <component name="RequiredTx" class="Seasar.Extension.Tx.TransactionInterceptor"> <arg> <component class="Seasar.Extension.Tx.Impl.DTCRequiredTxHandler" /> </arg> <property name="TransactionStateHandler">DTCTransactionStateHandler</property> </component> <component name="TransactionContext" class="Seasar.Extension.Tx.Impl.TransactionContext"> <property name="IsolationLevel">System.Data.IsolationLevel.ReadCommitted</property> </component> </components>
datasource2.dicon
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="datasource2"> <component name="NpgsqlProvider" class="Seasar.Extension.ADO.DataProvider"> <property name="ConnectionType">"Npgsql.NpgsqlConnection"</property> <property name="CommandType">"Npgsql.NpgsqlCommand"</property> <property name="ParameterType">"Npgsql.NpgsqlParameter"</property> <property name="DataAdapterType">"Npgsql.NpgsqlDataAdapter"</property> </component> <component name="NpgsqlDataSource" class="Seasar.Extension.Tx.Impl.TxDataSource"> <property name="DataProvider">NpgsqlProvider</property> <property name="ConnectionString"> "Server=192.168.0.100;Port=5432;User Id=hoge;Password=hogehoge;Database=hogeDB;Encoding=UTF-8" </property> </component> <component class="Seasar.Extension.ADO.Impl.BasicDataReaderFactory" /> <component class="Seasar.Extension.ADO.Impl.BasicCommandFactory" /> <component class="Seasar.Dao.Impl.FieldAnnotationReaderFactory" /> <component class="Seasar.Dao.Impl.DaoMetaDataFactoryImpl" /> <component name="DaoInterceptor" class="Seasar.Dao.Interceptors.S2DaoInterceptor"/> <component name="LocalRequiredTx" class="Seasar.Extension.Tx.TransactionInterceptor"> <arg> <component class="Seasar.Extension.Tx.Impl.LocalRequiredTxHandler" /> </arg> <property name="TransactionStateHandler">TransactionContext</property> </component> <component name="RequiredTx" class="Seasar.Extension.Tx.TransactionInterceptor"> <arg> <component class="Seasar.Extension.Tx.Impl.DTCRequiredTxHandler" /> </arg> <property name="TransactionStateHandler">DTCTransactionStateHandler</property> </component> <component name="TransactionContext" class="Seasar.Extension.Tx.Impl.TransactionContext"> <property name="IsolationLevel">System.Data.IsolationLevel.ReadCommitted</property> </component> </components>
dao1.dicon
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="dao1"> <include path="datasource1.dicon"/> <component name="EmployeeDao" class="HogeSystem.Dao.IEmployeeDao"> <aspect>datasource1.DaoInterceptor</aspect> </component> </components>
dao2.dicon
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="dao2"> <include path="datasource2.dicon"/> <component name="AppUserDao" class="HogeSystem.Dao.IAppUserDao"> <aspect>datasource2.DaoInterceptor</aspect> </component> </components>