Setting up mysql datasource in javaee
OK guys I know this looks stupid as many questions have already been asked but none of them solve my problem for setting up mysql datasource. I am using "Eclipse IDE", "MySql Database" and "java Language". I configured the context.xml as
<?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>
and then web.xml like
<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>
In the connection file name WorldCityBean.java I hava tried both ways: ie
annotating the class and using InitialContext, but it doesn't work at all.
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();
}
}
and giving this release
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
source to share
Using context.xml is not the standard way to configure a JDBC resource in Glassfish. While there are ways to configure it via the deployment descriptor ( fooobar.com/questions/1035246 / ... ), I would recommend using the web console. Log in, go to Resources and specify the Connection Pool and JDBC resource ( link for Glassfish 3.0.1 ). If you do, you can test / validate the SQL connection and verify that all parameters are correct. When you are sure that you have specified the correct parameters, a normal injection will suffice, but be careful - you must specify:
@Resource(name="jdbc/world")
instead
@Resource(name="world")
source to share