lzl123() 你好 根据下面的解释:Using the DataSource interface (JDBC 2.0) or the DriverManager (JDBC 1.0) interface, a J2EE component could get physical database connection objects. To obtain logical (pooled) connections, the J2EE component MUST use these JDBC 2.0 pooling manager interfaces: javax.sql.ConnectionPoolDataSource javax.sql.PooledConnection --------------------也就是说: DataSource 获得的不是从pool中获得的对吗? 而下面的代码才是:import java.sql.*; import javax.sql.*; // import here vendor specific JDBC driverspublic ProductPK ejbCreate() { try { // initialize JNDI lookup parameters Context ctx = new InitialContext(parms); ... ConnectionPoolDataSource cpds = (ConnectionPoolDataSource)ctx.lookup(cpsource); ... // Following parms could all come from a JNDI look-up cpds.setDatabaseName("PTDB"); cpds.setUserIF("XYZ"); ... PooledConnection pc = cpds.getPooledConnection(); Connection conn = pc.getConnection(); ... // do business logic conn.close(); } ... } lzl123() 我的理解对吗?
weblogic提供了连接池和数据源服务,我们可以在代码中直接使用定义好的数据源,但这样并不灵活,数据源的JNDI名一旦变动势必要引起程序的修改。一个比较好的选择是使用部署操作符的资源引用服务。以web编程为例,假设要在web端程序中直接使用数据库连接,可以在web.xml中声明如下:
<resource-ref>
<res-ref-name>dataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
声明中的dataSource可以视为一个编程接口,在程序中可以直接用它来取得数据库连接:
Context context = new InitialContext();
DataSource source = (DataSource)context.lookup("java:comp/env/dataSource");
source.getConnection();
dataSource跟实际的weblogic数据源可以通过weblogic.xml来对应:
<reference-descriptor>
<resource-description>
<res-ref-name>dataSource</res-ref-name>
<jndi-name>data source name</jndi-name>
</resource-description>
</reference-descriptor>
其中的data source name为真实的在weblogic配置的数据源名。通过这种方式,我们可以把开发跟部署分离开来,在部署期分配各个系统的数据库使用。
根据下面的解释:Using the DataSource interface (JDBC 2.0) or the DriverManager (JDBC 1.0) interface, a J2EE component could get physical database connection objects. To obtain logical (pooled) connections, the J2EE component MUST use these JDBC 2.0 pooling manager interfaces: javax.sql.ConnectionPoolDataSource
javax.sql.PooledConnection
--------------------也就是说:
DataSource 获得的不是从pool中获得的对吗?
而下面的代码才是:import java.sql.*;
import javax.sql.*;
// import here vendor specific JDBC driverspublic ProductPK ejbCreate() {
try {
// initialize JNDI lookup parameters
Context ctx = new InitialContext(parms);
...
ConnectionPoolDataSource cpds = (ConnectionPoolDataSource)ctx.lookup(cpsource);
...
// Following parms could all come from a JNDI look-up
cpds.setDatabaseName("PTDB");
cpds.setUserIF("XYZ");
...
PooledConnection pc = cpds.getPooledConnection();
Connection conn = pc.getConnection();
...
// do business logic
conn.close();
}
...
}
lzl123() 我的理解对吗?
ConnectionPoolDataSource我也没用过,不知道干嘛的:)