【问题标题】:Specify a `DataSource` factory instead of Tomcat's default指定一个 `DataSource` 工厂而不是 Tomcat 的默认值
【发布时间】:2019-10-15 00:11:36
【问题描述】:

tl;博士

如何告诉 Tomcat 9 使用 Postgres-specific object factory 生成 DataSource 对象以响应 JNDI 查询?

详情

通过定义一个与我的上下文名称相同的 XML 文件,我可以轻松地从 Apache Tomcat 9 获取一个 DataSource 对象。例如,对于一个名为 clepsydra 的网络应用程序,我创建了这个文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Domain: DEV, TEST, ACPT, ED, PROD  -->
    <Environment name = "work.basil.example.deployment-mode"
                 description = "Signals whether to run this web-app with development, testing, or production settings."
                 value = "DEV"
                 type = "java.lang.String"
                 override = "false"
                 />

    <Resource
                name="jdbc/postgres"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://127.0.0.1:5432/mydb"
                user="myuser"
                password="mypasswd"
                />
</Context>

我将该文件放在我的 Tomcat “base”文件夹中,在 conf 文件夹中,在我使用引擎名称 Catalina 和主机名 localhost 创建的文件夹中。 Tomcat 将设置输入资源工厂以返回DataSource 的实例。我可以通过 JNDI 访问该实例:

Context ctxInitial = new InitialContext();
DataSource dataSource = 
        ( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" )
;

我确实意识到该查找字符串中的postgres 可能是特定应用程序的特定内容。但是让我们使用postgres 进行相同的演示。

我想要org.postgresql.ds.PGSimpleDataSource,而不是org.apache.tomcat.dbcp.dbcp2.BasicDataSource

此设置使用Tomcat’s own resource factory for JDBC DataSource objects。返回的DataSource 类的基础类是org.apache.tomcat.dbcp.dbcp2.BasicDataSource。不幸的是,我不想要那个班级的DataSource。我想要JDBC driver from The PostgreSQL Global Development Group提供的类的DataSourceorg.postgresql.ds.PGSimpleDataSource

通过阅读 Tomcat 文档页面 JNDI Resources How-ToJNDI Datasource How-To,我开始意识到 Tomcat 允许我们为这些 DataSource 对象使用备用工厂来代替与 Tomcat 捆绑的默认工厂实现。听起来像我需要的。

PGObjectFactory

我发现 Postgres JDBC 驱动程序已经捆绑了这样的实现:

顺便说一句,OSGi 应用程序的驱动程序中内置了一个类似的工厂,PGDataSourceFactory。我认为这对我来说对 Tomcat 没有用。

所以,PGObjectFactory 类实现了 JNDI 所需的接口 javax.naming.spi.ObjectFactory

SPI

我猜这个包名中的spi 表示对象工厂通过Java Service Provider Interface (SPI) 加载。

所以我认为需要一个 SPI 映射文件,如 Oracle TutorialVaadin documentation 中所述。在我的 Vaadin resources 文件夹中添加了一个 META-INF 文件夹,并创建了一个进一步嵌套在那里的 services 文件夹。所以在/resources/META-INF/services 中,我创建了一个名为javax.naming.spi.ObjectFactory 的文件,其中包含一行文本,即我想要的对象工厂的名称:org.postgresql.ds.common.PGObjectFactory。我什至检查了 Postgres JDBC 驱动程序,以物理方式验证此类的存在和完全限定名称。

问题

➥ 我的问题是:我如何告诉 Tomcat 使用 PGObjectFactory 而不是它的默认对象工厂 来生成我的 DataSource 对象来生成与我的 Postgres 数据库的连接?

&lt;Resource&gt; 元素上的 factory 属性

我曾希望它就像在上面看到的 &lt;Resource&gt; 元素中添加 factory 属性 (factory="org.postgresql.ds.common.PGObjectFactory") 一样简单。我从 Tomcat 页面The Context Container 得到了这个想法。该页面非常令人困惑,因为它专注于全局资源,但我不需要或不想在全局范围内定义这个 DataSource。我只需要这个DataSource 用于我的一个网络应用程序。

添加 factory 属性:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Domain: DEV, TEST, ACPT, ED, PROD  -->
    <Environment name = "work.basil.example.deployment-mode"
                 description = "Signals whether to run this web-app with development, testing, or production settings."
                 value = "DEV"
                 type = "java.lang.String"
                 override = "false"
                 />

    <Resource
                name="jdbc/postgres"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://127.0.0.1:5432/mydb"
                user="myuser"
                password="mypasswd"
                factory="org.postgresql.ds.common.PGObjectFactory"
                />
</Context>

…失败,我的 DataSource 对象为空。

ctxInitial = new InitialContext();
DataSource dataSource = ( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" );
System.out.println( "dataSource = " + dataSource );

删除 factory="org.postgresql.ds.common.PGObjectFactory" 属性可解决异常。但后来我又回到了 Tomcat BasicDataSource 而不是 Postgres PGSimpleDataSource。因此我的问题在这里。

我知道我的 Context XML 已成功加载,因为我可以访问该 Environment 条目的值。

第二次实验

几天后,我从顶部尝试了这个。

我创建了一个名为“datasource-object-factory”的新“Plain Java Servlet”风格的 Vaadin 14.0.9 项目。

这是我的整个 Vaadin Web 应用程序代码。下半部分是 JNDI 查找。

package work.basil.example;

import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
@PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
public class MainView extends VerticalLayout
{

    public MainView ( )
    {
        Button button = new Button( "Click me" ,
                event -> Notification.show( "Clicked!" ) );


        Button lookupButton = new Button( "BASIL - Lookup DataSource" );
        lookupButton.addClickListener( ( ClickEvent < Button > buttonClickEvent ) -> {
            Notification.show( "BASIL - Starting lookup." );
            System.out.println( "BASIL - Starting lookup." );
            this.lookupDataSource();
            Notification.show( "BASIL - Completed lookup." );
            System.out.println( "BASIL - Completed lookup." );
        } );

        this.add( button );
        this.add( lookupButton );
    }

    private void lookupDataSource ( )
    {
        Context ctxInitial = null;
        try
        {
            ctxInitial = new InitialContext();

            // Environment entry.
            String deploymentMode = ( String ) ctxInitial.lookup( "java:comp/env/work.basil.example.deployment-mode" );
            Notification.show( "BASIL - deploymentMode: " + deploymentMode );
            System.out.println( "BASIL - deploymentMode = " + deploymentMode );

            // DataSource resource entry.
            DataSource dataSource = ( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" );
            Notification.show( "BASIL - dataSource: " + dataSource );
            System.out.println( "BASIL - dataSource = " + dataSource );
        }
        catch ( NamingException e )
        {
            Notification.show( "BASIL - NamingException: " + e );
            System.out.println( "BASIL - NamingException: " + e );
            e.printStackTrace();
        }
    }
}

为了简单起见,我没有指定 Tomcat“基本”文件夹,而是使用默认值。我没有从 IntelliJ 运行,而是手动将我的 Web 应用程序的 WAR 文件移动到 webapps 文件夹。

我下载了 Tomcat 的新版本,版本 9.0.27。我将 Postgres JDBC jar 拖到 /lib 文件夹中。我使用 BatChmod 应用程序来设置 Tomcat 文件夹的权限。

conf 文件夹中,我创建了Catalinalocalhost 文件夹。在那里,我创建了一个名为 datasource-object-factory.xml 的文件,其内容与上面看到的相同。

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Domain: DEV, TEST, ACPT, ED, PROD  -->
    <Environment name = "work.basil.example.deployment-mode"
                 description = "Signals whether to run this web-app with development, testing, or production settings."
                 value = "DEV"
                 type = "java.lang.String"
                 override = "false"
                 />

    <Resource
                factory="org.postgresql.ds.common.PGObjectFactory"
                name="jdbc/postgres"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://127.0.0.1:5432/mydb"
                user="myuser"
                password="mypasswd"
                />
</Context>

我将 Web 应用程序的 datasource-object-factory.war 文件复制到 Tomcat 中的 webapps。最后,我运行 Tomcat 的 /bin/startup.sh 并观察 WAR 文件分解到一个文件夹中。

使用Resource 元素上的factory="org.postgresql.ds.common.PGObjectFactory" 属性,生成的DataSourcenull

与我的第一个实验一样,我可以访问&lt;Environment&gt; 的值,因此我知道我的上下文名称 XML 文件正在被找到并通过 JNDI 成功处理。

以下是 Google Drive 上的日志:

【问题讨论】:

  • 抛出什么异常?注意你找到它here:在表格中描述但没有记录。
  • 但是根据我引用的文件以及您似乎正在引用的文件,您所做的应该有效。那么为什么它不起作用相关的。但如果你不想得到进一步的帮助,千万不要回答。
  • @user207421 当我添加了一个factory 属性时,我的异常发生了,因为我模糊地记得在某处看到过该属性。您链接的页面没有说明 Resource 元素上的 factory 属性。所以我不明白你为什么说“你所做的应该有效”。该文档中没有讨论我所做的事情。
  • 我链接的页面给出了一个例子。这就是为什么我说“描述但未在表格中记录”。您可以采取相当简单的步骤发布异常,或者您可以继续争论,但我警告您我会很快失去兴趣。
  • @user207421 我错了抛出异常。该异常来自我的网络应用程序失败,因为我预期的DataSource 对象是null。我还尝试为对象工厂添加 SPI 映射,但没有帮助。我用这些细节修改了我的问题。感谢您的关注和帮助。

标签: java tomcat servlets jndi objectfactory


【解决方案1】:

您的资源配置似乎需要修改。正如Tomcat Documentation中提到的,

您可以声明要为 JNDI 查找和 Web 应用程序部署描述符中的元素返回的资源的特征。 您还必须将所需的资源参数定义为 Resource 元素的属性,以配置要使用的对象工厂(如果 Tomcat 还不知道),以及使用的属性配置该对象工厂。

你得到null的原因是对象工厂无法确定它需要创建的对象类型,参考PGObjectFactory code

public Object getObjectInstance ( Object obj , Name name , Context nameCtx ,
                                  Hashtable < ?, ? > environment ) throws Exception
{
    Reference ref = ( Reference ) obj;
    String className = ref.getClassName();
    // Old names are here for those who still use them
    if ( 
            className.equals( "org.postgresql.ds.PGSimpleDataSource" )
            || className.equals( "org.postgresql.jdbc2.optional.SimpleDataSource" )
            || className.equals( "org.postgresql.jdbc3.Jdbc3SimpleDataSource" ) 
    )
    {
        return loadSimpleDataSource( ref );
    } else if ( 
            className.equals( "org.postgresql.ds.PGConnectionPoolDataSource" )
            || className.equals( "org.postgresql.jdbc2.optional.ConnectionPool" )
            || className.equals( "org.postgresql.jdbc3.Jdbc3ConnectionPool" ) 
    )
    {
        return loadConnectionPool( ref );
    } else if ( 
            className.equals( "org.postgresql.ds.PGPoolingDataSource" )
            || className.equals( "org.postgresql.jdbc2.optional.PoolingDataSource" )
            || className.equals( "org.postgresql.jdbc3.Jdbc3PoolingDataSource" ) 
    )
    {
        return loadPoolingDataSource( ref );
    } else
    {
        return null;
    }
}

资源定义中的值“javax.sql.DataSource”与对象工厂理解的任何类都不对应,请使用对象工厂理解的类之一,在您的情况下为“org.postgresql.ds.PGSimpleDataSource” '。

但是,这仍然无法为您提供有效的数据源,原因是,请在同一源代码中参考以下部分:

private Object loadSimpleDataSource(Reference ref) {
    PGSimpleDataSource ds = new PGSimpleDataSource();
    return loadBaseDataSource(ds, ref);
}

protected Object loadBaseDataSource(BaseDataSource ds, Reference ref) {
    ds.setFromReference(ref);

    return ds;
}

loadBaseDataSource在所有数据源的超类上调用setFromReference,参考:BaseDataSource,节:

public void setFromReference ( Reference ref )
{
    databaseName = getReferenceProperty( ref , "databaseName" );
    String portNumberString = getReferenceProperty( ref , "portNumber" );
    if ( portNumberString != null )
    {
        String[] ps = portNumberString.split( "," );
        int[] ports = new int[ ps.length ];
        for ( int i = 0 ; i < ps.length ; i++ )
        {
            try
            {
                ports[ i ] = Integer.parseInt( ps[ i ] );
            }
            catch ( NumberFormatException e )
            {
                ports[ i ] = 0;
            }
        }
        setPortNumbers( ports );
    } else
    {
        setPortNumbers( null );
    }
    setServerNames( getReferenceProperty( ref , "serverName" ).split( "," ) );

    for ( PGProperty property : PGProperty.values() )
    {
        setProperty( property , getReferenceProperty( ref , property.getName() ) );
    }
}

以上需要三个属性,即。 'databaseName'、'portNumber' 和 'serverName',所以这些属性也需要在资源定义中。

总而言之,您的资源声明可能应该如下所示:

<Resource
            factory="org.postgresql.ds.common.PGObjectFactory"
            name="jdbc/postgres"
            auth="Application"
            type="org.postgresql.ds.PGSimpleDataSource"
            serverName="127.0.0.1"
            portNumber="5432"
            databaseName="mydb"
            />

然后您应该像已经完成的那样解析数据源并使用 getConnection(userName, pwd) 获取连接。

注意:您还可以设置在BaseDataSource 中定义的“用户名”和“密码”属性。

综上所述,我们可以将您的原始示例修改为如下所示。我们使用了一些DataSource configuration properties defined by the Postgres JDBC driver

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- Domain: DEV, TEST, ACPT, ED, PROD  -->
    <Environment name = "work.basil.example.deployment-mode"
                 description = "Signals whether to run this web-app with development, testing, or production settings."
                 value = "DEV"
                 type = "java.lang.String"
                 override = "false"
                 />

    <!-- `DataSource` object for obtaining database connections to Postgres  -->
    <Resource
                factory="org.postgresql.ds.common.PGObjectFactory"
                type="org.postgresql.ds.PGSimpleDataSource"
                auth="Container"

                driverClassName="org.postgresql.Driver"
                name="jdbc/postgres"

                serverName="127.0.0.1"
                portNumber="5432"
                databaseName="myDb"

                user="myuser"
                password="mypasswd"

                ssl="false"
                />

</Context>

【讨论】:

  • 是的!那行得通。太感谢了。我花了很多时间来解决这个问题。希望这个页面也能帮助其他人。如此简单的解决方案,但从阅读文档中看并不明显。
猜你喜欢
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多