如何告诉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:org.postgresql.ds.PGSimpleDataSource
提供的课程中的DataSource
。
通过阅读Tomcat文档页面JNDI Resources How-To和JNDI Datasource How-To,我发现Tomcat允许我们为这些DataSource
对象使用替代工厂来代替默认工厂实现。与Tomcat捆绑在一起。听起来像我的需要。
PGObjectFactory
我发现Postgres JDBC驱动程序已经与以下实现捆绑在一起:
PGObjectFactory
PGXADataSourceFactory
DataSource
实现,用于分布式事务。顺便说一下,OSGi应用程序的驱动程序PGDataSourceFactory
中内置了一个类似的工厂。我认为这对Tomcat毫无用处。
因此,PGObjectFactory
类实现了JNDI所需的接口javax.naming.spi.ObjectFactory
。
我猜测该包名称中的spi
意味着对象工厂通过Java Service Provider Interface (SPI)加载。
因此,我假设需要一个SPI映射文件,如Oracle Tutorial和Vaadin 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
。我只需要一个DataSource
这个Web应用程序。
添加该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
条目的值。
几天后,我从顶部尝试了此操作。
我创建了一个新的“纯Java Servlet”类Vaadin 14.0.9项目,名为“数据源-对象-工厂”。
这是我完整的Vaadin网络应用代码。下半部分是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
文件夹中,创建了Catalina
和localhost
文件夹。在其中,我创建了一个名为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
属性的情况下,所得的DataSource
为null
。
与我的第一个实验一样,我可以访问<Environment>
的值,因此我知道我的上下文名称XML文件已找到并且可以通过JNDI成功处理。
以下是Google云端硬盘上的日志:
答案 0 :(得分:1)
您的资源配置似乎需要修改。如Tomcat Documentation中所述,
您可以在Web应用程序部署描述符中声明要为JNDI查找和元素返回的资源的特征。 您还必须定义所需的资源参数作为Resource元素的属性,以配置要使用的对象工厂 (如果Tomcat尚不知道)以及所使用的属性配置该对象工厂。
获取空值的原因是对象工厂无法确定需要创建的对象类型,请参考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>