【问题标题】:How can I use a datasource defined in Websphere Liberty Profile 8.5?如何使用 Websphere Liberty Profile 8.5 中定义的数据源?
【发布时间】:2013-07-31 02:23:01
【问题描述】:

我正在使用 WebSphere 8.5.5 Liberty 配置文件来部署一个应用程序,该应用程序使用通过 jndi 定义和公开的数据源。但我无法使用我的应用程序中的数据源。我的 server.xml 看起来像这样:

<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>jsp-2.2</feature>
        <feature>jndi-1.0</feature>
        <feature>ejbLite-3.1</feature>
        <feature>jdbc-4.0</feature>
    </featureManager>

    <dataSource id="mssqlserver" jndiName="jdbc/sqlserver_prod" type="javax.sql.DataSource">
        <jdbcDriver libraryRef="MSJDBCLib"/>
        <connectionManager numConnectionsPerThreadLocal="10" id="ConnectionManager" minPoolSize="1"/>
        <properties.microsoft.sqlserver username="sa" password="" databaseName="PROD"
                                    serverName="10.211.55.4" portNumber="1433"/>
    </dataSource>

    <library id="MSJDBCLib">
        <fileset dir="/Users/alter/Devel/sqlserver" includes="sqljdbc4.jar"/>
    </library>

    <httpEndpoint id="defaultHttpEndpoint"
                  host="0.0.0.0"
                  httpPort="9080"
                  httpsPort="9443" />
    <application id="ee1" location="/Users/alter/Devel/xxxx/src/ear/target/ee1-ear.ear" name="ear_ear_exploded" type="ear" >
        <classloader delegation="parentLast" commonLibraryRef="MSJDBCLib" />
    </application>

    <application id="ee1-web" location="/Users/alter/Devel/xxxx/src/web/target/ee1-web" name="web_exploded" type="war" />
</server>

我使用这个spring配置文件来注入数据源:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- START CONFIG DS -->

    <bean id="dataSourcePROD" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/sqlserver_prod"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourcePROD"/>
        <property name="mapperLocations" value="classpath*:mappers/**/*.xml"/>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="genericMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        <property name="addToConfig" value="true"/>
    </bean>
</beans>

Spring 能够从 JNDI 中找到我的“数据源”对象,但它无法将交付的对象转换为 javax.sql.Datasource。 实际的例外是:

org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.ws.jdbc.DataSourceService' to required type 'javax.sql.DataSource' for property 'dataSource';

注意 1:我没有提及可部署工件(ear、wars 等)中的 jdbc 驱动程序

我在 server.xml 中缺少一些用于数据源定义的配置参数??

【问题讨论】:

  • 我看不出您的配置有什么问题。该错误看起来像是产品问题。我建议与 IBM 建立 PMR。

标签: java spring datasource jndi websphere-liberty


【解决方案1】:

您可能想看看 Spring 助手类 WebSphereDataSourceAdapter。 IBM 有在服务中“包装”服务的诀窍,需要稍微“展开”才能使用。

查看 Spring API 文档中的 this 作为尝试的可能提示

【讨论】:

    【解决方案2】:

    我有类似的代码在工作,但我添加了 spring jee:jndi-lookup 标签,用于在 spring 中查找数据源。

    <jee:jndi-lookup id="dataSourcePROD" jndi-name="jdbc/sqlserver_prodS" resource-ref="false" expected-type="javax.sql.DataSource"/>
    

    【讨论】:

      【解决方案3】:

      usernameproperties.microsoft.sqlserver 元素中的错误属性。您应该改用 user

      <properties.microsoft.sqlserver user="sa" password="" databaseName="PROD"
                                      serverName="10.211.55.4" portNumber="1433"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-25
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多