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]



Reply via email to