我在Glassfish 3.1.2中创建了一个名为'userdb‘的连接池和一个名为'userdbresource’的数据源。在创建数据源时,我在管理控制台中将类型指定为'javax.sql.Datasource‘。
在我的REST webservice中,我编写了以下代码
DataSource ds = null;
try
{
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("userdbresource");
}
catch (NamingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = (Connection) ds.getConnection();
Statement stmt=con.createStatement();我在Connection con = (Connection) ds.getConnection();中总是得到一个ClassCast异常
我在web.xml中添加了以下内容
<resource-ref>
<res-ref-name>userdbresource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>但没有变化。在使用com.mysql.jdbc.Connection查找连接后,我尝试对其进行解包。但没有变化。
日志中的实际类广播异常是
java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.ConnectionHolder40 cannot be cast to com.mysql.jdbc.Connection如果有人知道我需要做哪些更改才能使用我从池中检索到的连接,请让我知道!
谢谢Kavita
发布于 2012-09-18 14:00:25
import java.sql.Connection;而不是jdbc连接,因此您将不需要任何连接转换,并且您的数据源连接将成为
Connect con = ds.getConnection();https://stackoverflow.com/questions/12471107
复制相似问题