• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JBoss DBCP

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jotnarta Jot
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jotnarta Jot
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help please???
 
Javid Jamae
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 '/')
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic