File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JBoss/WildFly and the fly likes JBoss DBCP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "JBoss DBCP" Watch "JBoss DBCP" New topic
Author

JBoss DBCP

Jotnarta Jot
Greenhorn

Joined: May 18, 2006
Posts: 24
Hi all

I am a very newbie to JBoss, and I am trying to build a small web application that connects to MySql schema. I know that for a scalable and effecient web site, you have to consider DBCP. I missed up here, I read alot of tutorials and articles around the net, but I couldn't reform this pieces of information to define a DBCP for mysql.

I end up confused, JNDI, Hibernate, Datasource???
Any one can help me with a small snippet of code, please???

I am using MySql 5.1.7, JBoss 5, and Eclipse 3.4.1....

Thank you in advance
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

DBCP??

For connection to MySQL, you will need a *-ds.xml file for MySQL. There is an example one at docs/examples/jca. Change it to match your database and give it a unique JNDI name.

In your app, you can look up the data source via the JNDI name, and from there get the JDBC session and from there on its is all JDBC.

Or you can work with a JPA entity bean, in which case you could use any EJB3 tutorial. For a complete example, see the source code for the book JBoss in Action (link is below). Chapter 7 includes several example. And chapter 3 has a Hibernate example.


JBoss In Action
Jotnarta Jot
Greenhorn

Joined: May 18, 2006
Posts: 24
Peter Johnson wrote:DBCP??

For connection to MySQL, you will need a *-ds.xml file for MySQL. There is an example one at docs/examples/jca. Change it to match your database and give it a unique JNDI name.

In your app, you can look up the data source via the JNDI name, and from there get the JDBC session and from there on its is all JDBC.


Thank you Peter. I changed the mysql-ds.xml file and I placed it in ../server/default/deploy. I looked up the JNDI name i provided as following:
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/MyProject");
Connection con = ds.getConnection();


But, when I run the file, I got the following error:
javax.servlet.ServletException: javax.naming.NameNotFoundException: MyProject not bound
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)


Is there anything that I missed it??
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
Please show us the contents of your mysql-ds.xml file. Also, please don't put your response inside of the Quote block. You can edit your previous message to fix that.


Author: JBoss in Action, javidjamae.com, @javidjamae on Twitter
Jotnarta Jot
Greenhorn

Joined: May 18, 2006
Posts: 24
Ok, first, this is the content of mysql-ds.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
<local-tx-datasource>
<jndi-name>MyProject</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/project</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>admin</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>


and this is the jsp code that I use to lookup the JNDI name, and to read from table questions (it's a small code for test purposes):
<%
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/MyProject");
Connection con = ds.getConnection();

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from questions");

while( rs.next() ) {
rs.getString("Description");
}
%>


and following is the error message after running this jsp file in JBoss:
javax.servlet.ServletException: javax.naming.NameNotFoundException: MyProject not bound
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.test_jsp._jspService(test_jsp.java:85)
............


Did I miss something???
Jotnarta Jot
Greenhorn

Joined: May 18, 2006
Posts: 24
Any help please???
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
I don't see anything immediately wrong, so perhaps it didn't bind to JNDI because there was an error connecting to the DB. Have you looked through your server.log file to see if there were any errors.

This is what I see when I start JBoss 5 with the standard datasource (hsqldb):



You should see some similar messages that confirm that your datasource was bound to JNDI.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Jotnarta, please do not use font colors for your posts - that makes the text difficult to read. Instead, use the Code button to highlight the source code you post.

Also, try using the name "java:MyProject" (no '/')
vikash mishra
Greenhorn

Joined: Jun 16, 2009
Posts: 4
Hi,
I am not sure if you have missed this but you can still go ahead and setup default/conf/jboss-service.xml if you have not done yet.


<attribute name="SessionFactoryName">
java:/hibernate/ExampleSessionFactory
</attribute>
You can change the text which is bold with your own specific jndi name. then your code will able to get the hibernate sessionFactotry.
Thanks and Regards
Vikash Chandra Mishra
 
 
subject: JBoss DBCP