David, hope this helps: <data-sources> <data-source type="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> <set-property property="autoCommit" value="true"/> <set-property property="description" value="MySql Data Source"/> <set-property property="driverClass" value="com.mysql.jdbc.Driver"/> <set-property property="maxCount" value="10"/> <set-property property="minCount" value="2"/> <set-property property="user" value="dbuser"/> <set-property property="password" value="xxxxx"/> <set-property property="url" value="jdbc:mysql://localhost/collection"/> </data-source> </data-sources> I'm using the latest stable mysql driver mysql-connector-java-3.0.9-stable-bin.jar.
This is not the best example since it accesses the db from within a jsp: Connection conn = null; DataSource dataSource = null; try { dataSource = (DataSource) getServletContext().getAttribute(Action.DATA_SOURCE_KEY); conn = dataSource.getConnection(); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery( "select count(*) from collection.web_users"); if (rs.next()) { %> Number of users defined= <%=rs.getInt(1) %> <% } } catch (SQLException sqle) { log("Connection.process", sqle); } finally { //enclose this in a finally block to make //sure the connection is closed try { if (conn != null) conn.close(); } catch (SQLException e) { log("Connection.close", e); } } %> met vriendelijke groeten, salutations sincères, kind regards, Geert Van Landeghem Reynders Etiketten NV tel: +32 3 460.32.81 gsm: +32 477.75.95.33 <mailto:[EMAIL PROTECTED]> Give me the power to accept the things i cannot change, the strength to change the things i can change, and the insight to tell the difference -----Original Message----- From: David Parry [mailto:[EMAIL PROTECTED] Sent: donderdag 6 november 2003 14:41 To: [EMAIL PROTECTED] Subject: data-source connection keeps closing After days of googling and trawling through the mail archives, I still can't find any explanation for this... I'm trying to get a DataSource that's been configured in struts-config.xml and use the connection in my Model. Every time I try to use the connection it says that it has closed (before even getting the chance to create a statement!). The connection that is returned sure enough has the correct URL, user, password & driver, and independent tests have confirmed that mysql is running, that permissions are acceptable for that user, it's accessible from Java, blah blah... I've looked through most of the examples listed on the struts resources pages, but I can't seem to find anything that has a simple direct-to-jdbc example. Currently I'm not in a position to be using anything like Hibernate, etc, so all those examples are kinda not helpful in my situation. ******** QUESTION: Does anyone have a simple canonical example of connecting to a JDBC database (preferably MySQL) via struts? ******** I'm using Tomcat 4.1, MySQL 4.1.0, MySQL Connector 3.0.9 and struts 1.2. Error is as follows: 14:21:44,750 DEBUG QueryAction:55 - dataSource = [EMAIL PROTECTED] 14:21:45,561 DEBUG QueryAction:59 - myConnection = [EMAIL PROTECTED] 14:21:45,636 DEBUG QueryAction:61 - queryModel = [EMAIL PROTECTED] 14:21:45,677 DEBUG Model:109 - findNodes(null) 14:21:45,682 DEBUG Model:384 - readAll(node) 14:21:45,687 DEBUG Model:385 - connection = [EMAIL PROTECTED] 14:21:45,712 ERROR Model:392 - SQL Exception java.sql.SQLException: Connection is closed. at org.apache.commons.dbcp.DelegatingConnection.checkOpen(DelegatingConnect ion.java:245) at org.apache.commons.dbcp.DelegatingConnection.createStatement(DelegatingC onnection.java:170) at com.imc.semantic.Model.readAll(Model.java:389) struts-config.xml: <data-sources> <data-source type="org.apache.commons.dbcp.BasicDataSource"> <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver"/> <set-property property="url" value="jdbc:mysql://localhost/semantic"/> <set-property property="username" value="root"/> <set-property property="password" value=""/> <set-property property="maxActive" value="10" /> <set-property property="maxWait" value="5000" /> <set-property property="defaultAutoCommit" value="false" /> <set-property property="defaultReadOnly" value="false" /> <set-property property="validationQuery" value="SELECT COUNT(*) FROM node" /> </data-source> </data-sources> and the Java code in the Action: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DataSource dataSource; QueryModel queryModel = null; Connection myConnection = null; try { dataSource = getDataSource(request); // dataSource = servlet.findDataSource(null); logger.debug("dataSource = " + dataSource); if (dataSource != null) { myConnection = dataSource.getConnection(); logger.debug("myConnection = " + myConnection); queryModel = new QueryModel(myConnection); logger.debug("queryModel = " + queryModel); } else { throw new Exception("dataSource was null"); } } catch (SQLException e) { getServlet().log("Connection.process", e); } finally { // enclose this in a finally block // to make sure the connection is closed try { if (myConnection != null) myConnection.close(); } catch (SQLException e2) { getServlet().log("Connection.close", e2); } } ------------------- David Parry Pronoia: the irrational feeling that forces are working behind your back to make your life better. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]