3

好吧,伙计们,我知道这看起来很愚蠢,因为已经问了很多问题,但是没有一个解决了我配置 mysql 数据源的问题。我正在使用“Eclipse IDE”、“MySql 数据库”和“java 语言”。我已将 context.xml 配置为

<?xml version="1.0" encoding="UTF-8"?>
<Context>
 <Resource name="jdbc/world" auth="Container" type="javax.sql.DataSource"
           maxActive="50" maxIdle="30" maxWait="10000"
           username="root" password="admin" 
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/world"/>
</Context>

然后 web.xml 作为

<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/world</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

在连接文件名 WorldCityBean.java 我尝试了两种方式,即

通过注释类和使用 InitialContext 但它根本不起作用。

public class WorldCityBean {

@Resource(name="world")
private DataSource ds;

private Connection conn;

public void open() throws SQLException, NamingException{
    if(conn !=null)
    {
        return;
    }

//      Context ic = new InitialContext();
//      Context subcontext = (Context) ic.lookup("java:comp/env");
//      DataSource ds = (DataSource) subcontext.lookup("jdbc/world");

    conn = ds.getConnection();
}
}

并给出这个例外

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from     fulfilling this request.

exception 
javax.servlet.ServletException: An error occurred performing resource injection on     managed bean city

root cause 
com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing     resource injection on managed bean city

root cause 
com.sun.faces.spi.InjectionProviderException:         com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to     inject Res-Ref-Env-Property: world@javax.sql.DataSource@ resolved as: jndi:     world@res principal: null@mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class com.corejsf.WorldCityBean

root cause 
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting     to inject Res-Ref-Env-Property: world@javax.sql.DataSource@ resolved as:     jndi: world@res principal: null@mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class com.corejsf.WorldCityBean

root cause 
javax.naming.NamingException: Lookup failed for 'java:comp/env/world' in             SerialContext  [Root exception is javax.naming.NamingException: Lookup failed for 'world'     in SerialContext  [Root exception is javax.naming.NameNotFoundException: world not found]]

root cause 
javax.naming.NamingException: Lookup failed for 'world' in SerialContext  [Root     exception is javax.naming.NameNotFoundException: world not found]

root cause 
javax.naming.NameNotFoundException: world not found

note The full stack traces of the exception and its root causes are available in the     GlassFish Server Open Source Edition 3.0.1 logs.

--------------------------------------------------------------------------------

GlassFish Server Open Source Edition 3.0.1
4

1 回答 1

1

使用 context.xml 不是在 Glassfish 中配置 JDBC 资源的标准方法。虽然有一些方法可以通过部署描述符(Stackoverflow 链接)来配置它,但我还是推荐使用 Web 控制台。登录,转到资源并定义连接池和 JDBC 资源(Glassfish 3.0.1 的链接)。如果您这样做,您可以测试/ping 您的 SQL 连接并检查所有参数是否正确。当你确定你指定了正确的参数时,通常的注入就足够了,但要小心——你必须指定:

@Resource(name="jdbc/world")

代替

@Resource(name="world")
于 2013-01-26T11:22:51.657 回答