加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 以编程方式为Postgres JDBC生成一个`DataSource`

发布时间:2020-12-13 18:07:01 所属栏目:百科 来源:网络整理
导读:JDBC Tutorial建议使用 DataSource 对象来获取数据库连接,而不是使用DriverManager类.引用 Connecting with DataSource Objects 页面: DataSource objects … the preferred means of getting a connection to a data source. 如何获得与Postgres JDBC连接
JDBC Tutorial建议使用 DataSource对象来获取数据库连接,而不是使用DriverManager类.引用 Connecting with DataSource Objects页面:

DataSource objects … the preferred means of getting a connection to a data source.

如何获得与Postgres JDBC连接的此类对象?我有一个JDBC驱动程序.

现在,我不想像this或this那样摆弄JNDI.

我可以在Java应用程序中以编程方式实例化DataSource吗?或者我必须自己实现该DataSource接口吗?

JDBC驱动程序的实现

您的JDBC driver可能会为您提供DataSource接口的实现.

此实现的对象包含建立和配置与数据库的连接所需的信息,例如:

>姓名&数据库用户的密码
> IP地址&数据库服务器的端口号

最多可提供三种实施方案:

>通常这样的实现是围绕DriverManager的薄包装.每次在这样的实现的对象上调用DataSource::getConnection时,您将获得新的数据库连接.
>或者,实现可能使用下面的connection pool来提供已存在的连接.这些连接被分发并重新检入,如图书馆中的书籍,以便重复使用.
>实现可以支持Java Transaction API,支持X/Open XA,以满足复杂的需求,例如协调跨多个资源(如数据库和消息队列)的事务.不常用,所以我在这里忽略这种类型.

来自jdbc.postgresql.org的驱动程序

从jdbc.postgresql.org开始,开源免费驱动程序提供了所有三种类型的DataSource实现.但是作者不建议在生产中实际使用他们的connection pool type;如果要池化,请使用第三方连接池库.我们忽略了the XA type.

那么让我们看一下DataSource的简单的每次连接实现:org.postgresql.ds.PGSimpleDataSource

配置数据源对象

实例化一个空对象,然后调用一系列setter methods来配置您的特定数据库方案. setter方法继承自org.postgresql.ds.common.BaseDataSource.

我们还没有向接口DataSource上传,因此我们可以调用various setter methods.请参阅Data Sources and JNDI页面上的示例代码和讨论.

PGSimpleDataSource ds = new PGSimpleDataSource() ;  // Empty instance.
ds.setServerName( "localhost" );  // The value `localhost` means the Postgres cluster running locally on the same machine.
ds.setDatabaseName( "testdb" );   // A connection to Postgres must be made to a specific database rather than to the server as a whole. You likely have an initial database created named `public`.
ds.setUser( "testuser" );         // Or use the super-user 'postgres' for user name if you installed Postgres with defaults and have not yet created user(s) for your application.
ds.setPassword( "password" );     // You would not really use 'password' as a password,would you?

通常我会使用这些单独的setter方法.或者,您构造一个String,一个URL,其中包含要在一个笔划中在DataSource上设置的各种信息.如果你想去那条路线,请拨打setUrl.

这涵盖了基础知识.但是你可能想要或需要一些其他的制定者.其中大多数是在服务器上设置Postgres property值.这些属性都具有智能默认值,但您可能希望覆盖特殊情况.

ds.setPortNumber( 6787 ) ;  // If not using the default '5432'.
ds.setApplicationName( "whatever" ) ;   // Identify the application making this connection to the database. Also a clever way to back-door some information to the Postgres server,as you can encode small values into this string for later parsing. 
ds.setConnectTimeout( … ) ;  // The timeout value used for socket connect operations,in whole seconds. If connecting to the server takes longer than this value,the connection is broken.
ds.setSocketTimeout( … ) ;  // The timeout value used for socket read operations. If reading from the server takes longer than this value,the connection is closed. This can be used as both a brute force global query timeout and a method of detecting network problems.
ds.setReadOnly( boolean ) ;  // Puts this connection in read-only mode.

如果使用TLS(以前称为SSL)加密数据库连接以防止窃听或恶意操作,请使用多个setter.

对于没有特定setter方法的任何Postgres属性,您可以致电setProperty( PGProperty property,String value ).

您可以通过调用任何多种getter方法来检查或验证此数据源上的设置.

配置PGSimpleDataSource后,您可以将其作为DataSource对象传递给代码库的其余部分.这使您的代码库免于更改为另一个DataSource实现或更改为another JDBC driver的冲击.

DataSource dataSource = ds ;  // Upcasting from concrete class to interface.
return dataSource ;

使用数据源

使用DataSource非常简单,因为它只提供了两种方法,getConnection上的一对变体可以为数据库工作获取Connection对象.

Connection conn = dataSource.getConnection() ;

完成连接后,最佳做法是确保关闭它.使用try-with-resources syntax自动关闭连接,或明确关闭连接.

conn.close() ;

请记住,DataSource实际上并不是数据源. DataSource实际上是生成/访问数据库连接的源.在我看来,这是一个误称,因为我认为它是ConnectionSource. DataSource只与您的数据库通信足够长的时间以使用用户名和密码登录.登录后,使用Connection对象与数据库进行交互.

存储您的数据源

配置完成后,您希望保留该数据源对象,缓存.无需重复重新配置. implementation should be written to be thread-safe.您可以随时随地致电getConnection.

对于简单的小型Java应用程序,您可能希望将其作为字段存储在单例或静态全局变量中.

对于基于Servlet的应用程序(如Vaadin应用程序),您将创建一个实现ServletContextListener接口的类.在该类中,您将在Web应用程序启动时建立您的DataSource对象.从那里你可以通过传递到setAttribute将对象存储在ServletContext对象中.上下文是’web app’的技术术语.通过调用getAttribute并转换为DataSource来检索.

在企业方案中,DataSource可以存储在符合JNDI的实现中.诸如Apache Tomcat的一些Servlet containers可以提供JNDI实现.有些组织使用服务器,例如LDAP server.注册&使用JNDI检索DataSource对象包含在许多其他问题和解决方案中.堆栈溢出的答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读