gpt4 book ai didi

java - 指定`DataSource`工厂而不是Tomcat的默认工厂

转载 作者:行者123 更新时间:2023-11-28 22:46:36 26 4
gpt4 key购买 nike

tl; dr

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

细节

通过定义一个与上下文相同名称的XML文件,我可以轻松地从DataSource 9中获得一个Apache Tomcat对象。例如,对于名为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“基本”文件夹中,在 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驱动程序已经与以下实现捆绑在一起:


PGObjectFactory用于简单的JDBC连接。
PGXADataSourceFactory对于启用 XADataSource实现,用于分布式事务。


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

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

SPI

我猜该包名称中的 spi意味着对象工厂通过 Java Service Provider Interface (SPI)加载。

screenshot of location of SPI mapping file, and its contents

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



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

factory元素上的 <Resource>属性

我曾希望它将像在上面看到的 factory元素中添加 factory="org.postgresql.ds.common.PGObjectFactory"属性( <Resource>)一样简单。我从Tomcat页面 The Context Container中得到了这个想法。该页面着重于全局资源,因此非常混乱,但是我不需要或不想全局定义此 DataSource。我仅对一个Web应用程序需要此 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对象为null失败。

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条目的值。

第二次实验

几天后,我从顶部尝试了此操作。

我创建了一个新的“纯Java Servlet”类Vaadin 14.0.9项目,名为“ datasource-object-factory”。

这是我完整的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文件爆炸到一个文件夹中。

在我的 factory="org.postgresql.ds.common.PGObjectFactory"元素上具有 Resource属性时,生成的 DataSourcenull

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

以下是Google云端硬盘上的日志:


catalina.out
catalina.2019-10-18.log

最佳答案

您的资源配置似乎需要修改。如Tomcat Documentation中所述,


  您可以在Web应用程序部署描述符中声明要为JNDI查找和元素返回的资源的特征。您还必须定义所需的资源参数作为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中定义的'userName'和'password'属性。

综上所述,我们可以将您的原始示例修改为如下所示。我们使用一些 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>

关于java - 指定`DataSource`工厂而不是Tomcat的默认工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com