相关疑难解决方法(0)

关闭池中的JDBC连接

我们使用JDBC的标准代码部分是......

Connection conn = getConnection(...);
Statement  stmt = conn.conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
                                                ResultSet.CONCUR_READ_ONLY);
ResultSet  rset = stmt.executeQuery (sqlQuery);

// do stuff with rset

rset.close(); stmt.close(); conn.close();
Run Code Online (Sandbox Code Playgroud)

问题1:使用连接池时,是否应该在结束时关闭连接?如果是这样,汇集失败的目的不是?如果没有,DataSource如何知道什么时候释放Connection的特定实例并且可以重用?我对这个有点困惑,任何指针都赞赏.

问题2:以下方法是否接近标准?看起来是尝试从池中获取连接,如果无法建立DataSource,请使用旧式DriverManager.我们甚至不确定哪个部分在运行时被执行.重复上面的问题,是否应该关闭连接出来的这种方法?

谢谢, - MS.

synchronized public Connection getConnection (boolean pooledConnection)
                                                        throws SQLException {
        if (pooledConnection) {
                if (ds == null) {
                        try {
                                Context envCtx = (Context)
                                        new InitialContext().lookup("java:comp/env");
                                ds = (DataSource) envCtx.lookup("jdbc/NamedInTomcat");
                                return ds.getConnection();
                        } catch (NamingException e) {
                                e.printStackTrace();
                }}
                return (ds == null) ? getConnection (false) : ds.getConnection();
        }
        return DriverManager.getConnection(
                "jdbc:mysql://"+ipaddy+":"+dbPort …
Run Code Online (Sandbox Code Playgroud)

java mysql connection-pooling jdbc

104
推荐指数
2
解决办法
8万
查看次数

在WebSphere 8.5中查找JDBC DataSource

我想在我的webapp中使用JDBC Connetion,这是在WebSphere中配置的.(就像这里的那个:如何在JavaEE中使用JDBC?)

我之前通过JPA使用过这个DataSource,但是我们的客户希望拥有原生SQL ...不要问.

我发现很多的例子和教程(如http://www.wickcentral.com/java/dl/ds_resreferencesetts_Websphere.pdf,的Websphere JNDI查找失败),但不想要的工作.

WebSphere中的DataSource具有JNDI名称"jdbc/myDS"

我在我的web.xml中添加了一个resource-ref:

<resource-ref>
    <res-ref-name>jdbc/myDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
Run Code Online (Sandbox Code Playgroud)

我试着在我的Dao中获取DataSource:

ds = (DataSource) new InitialContext()
                .lookup("java:comp/env/jdbc/myDS");
Run Code Online (Sandbox Code Playgroud)

但我得到的是一个

 com.ibm.wsspi.injectionengine.InjectionException: CWNEN0044E: A resource reference binding could not be found for the following resource references [jdbc/myDS], defined for the MyAPP component.
Run Code Online (Sandbox Code Playgroud)

我尝试了很多.有没有人看到这个错误?

java websphere jndi jdbc java-ee

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×2

jdbc ×2

connection-pooling ×1

java-ee ×1

jndi ×1

mysql ×1

websphere ×1