- This topic has 9 replies, 3 voices, and was last updated 18 years, 11 months ago by David.
-
AuthorPosts
-
DavidMemberI have a Struts 1.1 application and I am trying to decide what the best route for database connectivity is. Pre-Struts 1.1 provides a <DataSource> but that is discouraged in 1.2, and that is not something that “feels right”. I am working on Tomcat 4.x and will migrate to Tomcat 5.0 very soon, so I think maybe their DBCP package is the way to go.
Can anyone recommend an approach that will be better than my database service implementing a Database pooling directly? Is DBCP going to be able to handle pooling, time-outs etc?
I have Google’d and searched for better solutions, perhaps someone here has a good opinion?
Riyad KallaMemberMoving to OT > Soft Dev
sonoerin,
What you want to do is setup a “Data source” in Tomcat, this is exactly what they are for. Let the app server handle pooling connections from the data source, and just have your application get a reference to it via JNDI.
DavidMemberThank you for the advice, perhaps you can take a look and see what I am doing wrong here:
Here is the error message I am getting:
Cannot create JDBC driver of class ” for connect URL ‘null’Here is my code to look up the datasource:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup(“java:/comp/env”);
DataSource ds = (DataSource)envContext.lookup(“jdbc/jsidb”);
return ds.getConnection();Here is the server.xml
<Resource name=”jdbc/jsidb” auth=”Container” type=”javax.sql.DataSource”
maxActive=”100″ maxIdle=”30″ maxWait=”10000″
username=”user” password=”password” driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://127.0.0.1:3306/jsi_db?autoReconnect=true”/>here is my web.xml:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/jsidb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>I have looked thru lots of online articles and even my Tomcat Book, but I don’t see what I am doing wrong here.
thank you for any help or advice you can offer.
Scott AndersonParticipantDo you have a MySQL JDBC driver jar on your deployed application’s classpath?
DavidMemberYes, I have mysql-connector-java-3.1.10-bin.jar in the /common/lib. If I use regular lookup code (hardcoded values to load the drivers and create the connections) that seems to work okay.
Riyad KallaMembersonoerin,
Are you getting a NullPointerException, or some other kind of error? You don’t need to do this:
Context envContext = (Context)initContext.lookup(“java:/comp/env”);
DataSource ds = (DataSource)envContext.lookup(“jdbc/jsidb”);one single line should be enough
DavidMemberI changed my lookup to :
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup(“jdbc/jsidb”);When I try to use it, I get the following:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:764)
at org.apache.naming.NamingContext.lookup(NamingContext.java:147)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.jsi.services.DataBaseService.getConnection(DataBaseService.java:84)
…etc…
DavidMemberI have done extensive Googling on this issue and it seems to be a common issue with Tomcat, if not configuring servers in general. This would be a really great feature of myEclipse, the time savings for people new to Tomcat would incredible.
anyway, if I go with my original configuration I get the no driver found for null url, or if I go with the other I get the “name jdbc not bound to this context” What am I missing here, it has to be something really obvious since my code is basically a copy of every example on the internet
Scott AndersonParticipantI found a few links that look promising for you:
http://forum.java.sun.com/thread.jspa?threadID=567630&tstart=15
http://www.theserverside.com/discussions/thread.tss?thread_id=25459
http://www.netbeans.org/kb/faqs/database-connectivity.htmlHope one of these helps.
DavidMemberThank you, I was able to get this working on Tomcat 4 finally – turned out to be a context problem. But now that I am trying to get it on Tomcat 5.0.28 I am right back to the same issue. Using the Tomcat Admin tool I can see the correct DataSource, but when I look at the Host context for my project, I see the JNDI name, but it doesn’t have the JDBC driver name. When I add those, it doesn’t save them.
I am getting the Caused By java.sql.SQLException: No suitable Driver found , yet I can see that the mysql driver is in /common/lib
here is my tomcat 5 server.xml:
<Resource name=”jdbc/jsidb” type=”javax.sql.DataSource”/>
<ResourceParams name=”jdbc/jsidb”>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/jsi_db?autoReconnect=true</value>
</parameter>
<parameter>
<name>password</name>
<value>jsi</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>jsi</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
</ResourceParams>According to the links above and the Tomcat 5 documentation, this should work right?
-
AuthorPosts