[Seasar-user:13918] [S2JDBC] 複数のデータソースの使い方について

[E-MAIL ADDRESS DELETED] [E-MAIL ADDRESS DELETED]
2008年 4月 24日 (木) 16:07:51 JST


お世話になります。中村と申します。
複数のデータソースの使い方について、ご教示をお願い致します。

S2JDBCドキュメント・「その他の機能 複数のデータソース」の記述を参考にし、JdbcManagerを、
@Bindingアノテーションを使って取得しようとしていますが、うまく取得できません。
なお、同じS2Containerの定義(dicon)を使用し、S2Container#getComponentを使用した場合は、
正常に取得できます。
動作環境は以下です。
初歩的な問題かも知れませんが、よろしくお願い致します。

====s2jdbc.dicon===========================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
      "http://www.seasar.org/dtd/components24.dtd">
<components>
      <include path="s2jdbca.dicon"/>
      <include path="s2jdbcb.dicon"/>
</components>

====s2jdbca.dicon===========================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
      "http://www.seasar.org/dtd/components24.dtd">
<components namespace="s2jdbcA">
      <include path="jdbca.dicon"/>
      <include path="s2jdbc-internal.dicon"/>
      <component name="jdbcManagerA" class="org.seasar.extension.jdbc.manager.JdbcManagerImpl">
            <property name="maxRows">0</property>
            <property name="fetchSize">0</property>
            <property name="queryTimeout">0</property>
            <property name="dialect">mysqlDialect</property>
            <property name="dataSource">DataSource</property>
      </component>
</components>

====s2jdbcb.dicon===========================================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
      "http://www.seasar.org/dtd/components24.dtd">
<components namespace="s2jdbcB">
      <include path="jdbcb.dicon"/>
      <include path="s2jdbc-internal.dicon"/>
      <component name="jdbcManagerB" class="org.seasar.extension.jdbc.manager.JdbcManagerImpl">
            <property name="maxRows">0</property>
            <property name="fetchSize">0</property>
            <property name="queryTimeout">0</property>
            <property name="dialect">mysqlDialect</property>
            <property name="dataSource">DataSource</property>
      </component>
</components>

====jdbca.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="jdbc">
      <include path="jta.dicon"/>

      <component name="xaDataSource"
            class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
            <property name="driverClassName">
                  "com.mysql.jdbc.Driver"
            </property>
            <property name="URL">
                  "jdbc:mysql://localhost:3306/testa"
            </property>
            <property name="user">"user"</property>
            <property name="password">"user"</property>
      </component>

      <component name="connectionPool"
            class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
            <property name="timeout">600</property>
            <property name="maxPoolSize">10</property>
            <property name="allowLocalTx">true</property>
            <destroyMethod name="close"/>
      </component>

      <component name="DataSource"
            class="org.seasar.extension.dbcp.impl.DataSourceImpl"
      />

</components>

====jdbcb.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="jdbc">
      <include path="jta.dicon"/>

      <component name="xaDataSource"
            class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
            <property name="driverClassName">
                  "com.mysql.jdbc.Driver"
            </property>
            <property name="URL">
                  "jdbc:mysql://localhost:3306/testb"
            </property>
            <property name="user">"user"</property>
            <property name="password">"user"</property>
      </component>

      <component name="connectionPool"
            class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
            <property name="timeout">600</property>
            <property name="maxPoolSize">10</property>
            <property name="allowLocalTx">true</property>
            <destroyMethod name="close"/>
      </component>

      <component name="DataSource"
            class="org.seasar.extension.dbcp.impl.DataSourceImpl"
      />

</components>

====テストプログラム(@Bindingアノテーションを使用)======================================
package examples.entity;

import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.extension.unit.S2TestCase;
import org.seasar.framework.container.annotation.tiger.Binding;

public class GetJdbcManagerTest extends S2TestCase {
    @Binding("jdbcManagerB")
    private JdbcManager jdbcManager;

    @Override
    protected void setUp() throws Exception {
        include("app.dicon");
    }

    public void testGetMessage() throws Exception {
        Message message = jdbcManager.from(Message.class)
            .where("id = ?", 1).getSingleResult();
        System.out.println("message="+message.getMessage());
    }
}
→→→message=aaaaと表示されます。

====テストプログラム(S2Container#getComponentを使用)======================================
package examples.entity;

import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.extension.unit.S2TestCase;
import org.seasar.framework.container.S2Container;

public class GetJdbcManagerTest extends S2TestCase {
    private S2Container container;
    private JdbcManager jdbcManager;

    @Override
    protected void setUp() throws Exception {
        include("app.dicon");
    }

    public void testGetMessage() throws Exception {
        JdbcManager jdbcManager = (JdbcManager) container.getComponent("jdbcManagerB");
        Message message = jdbcManager.from(Message.class)
            .where("id = ?", 1).getSingleResult();
        System.out.println("message="+message.getMessage());
    }
}
→→→message=bbbbと表示されます。

====Message.java=======================================================================
package examples.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Message {

    @Id
    @GeneratedValue
    private Integer id;

    private String message;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
====データベース内容====================================================================
MySQL
 DB:testa table:message
  id:1 message:aaaa
 DB:testb table:message
  id:1 message:bbbb





Seasar-user メーリングリストの案内