problem connection mysql with struts
hi,
i have a problem with my mysql database connection with struts. this is a part of my struts-config.xml:
<data-sources>
<data-source key="domao" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/domao" />
<set-property property="user" value="root" />
<set-property property="password" value="8qmkx33" />
<set-property property="maxActive" value="10" />
<set-property property="maxWait" value="5000" />
<set-property property="defaultAutoCommit" value="false" />
<set-property property="defaultReadOnly" value="false" />
</data-source>
</data-sources>
This is the instruction to try to use my database through the key "domao":
DataSource datasource = (DataSource)servlet.getServletContext().getAttribute("domao");
the compilation of my source java is ok but when I access the web page wich use this fonction, datasource=null
when I use in the same source java a connection like this:
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();//chargement du driver de connection JDBC pour MySQL
validSql = true;
}catch(Exception e1){
System.out.println("impossible de charger le pilote JDBC");
}
if(validSql){
validSql = false;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/domao", "root", "8qmkx33");//connexion 脿 la base
validSql = true;
...
everything is ok
I don't understand why I can't use my database with a <data-source> declaration in the struts-config.xml.
PS: I use tomcat 4.1.31, struts 1.2, mysql-connector-java-3.0.17
I also try these syntax but I still have error:
DataSource datasource = (DataSource)this.getDataSource(request, "domao");
DataSource datasource = (DataSource)request.getAttribute("domao");
DataSource datasource = (DataSource)getDataSource(request, "domao");
...
In fact, I tried a lot of things but not the one that is OK!!!
if someone could help me...
thank you very mutch
cedric pujol
[2305 byte] By [
cedbxa] at [2007-10-3 8:55:55]

hello, first of all, I want to say thank you for your reply.
I'm very disappointed with my problem. I've tried it to work for five days now. I have a question about your answer... you think that it could be better for me to declare my datasource in the tomcat configuration file instead of declaring it in the struts configuration file? that's it?
or, you say that I need to declare my datasource in the both?
I don't understand everything you said perhaps because my english is not very good...
can you confirm your point of view...
best regards
cedric pujol
france
> I'm very disappointed with my problem. I've tried it
> to work for five days now. I have a question about
> your answer... you think that it could be better for
> me to declare my datasource in the tomcat
> configuration file instead of declaring it in the
> struts configuration file?
Yes, Tomcat is responsible for maintaining the connection pool. If you don't tell Tomcat how to do it, you won't get one. I don't believe Struts maintains connection pools.
> or, you say that I need to declare my datasource in the both?
I don't know what Struts requires, but Tomcat does have to know about the data source parameters in order to maintain it.
> I don't understand everything you said perhaps
> because my english is not very good...
Your English is fine. It's far better than my French... 8)
I'd also recommend that you not put database code directly into Struts Action classes. In that case, there's no need for Struts to know about the data source.
%
I'm new to struts .When using may s data source code in struts-config file, I got the exception , when loading tomcat:
exc: is Unable to initialize struts ActionServlet due to an unexpected or
error thrown, so making the servlet as unavailable. Most likely, this is due to an incorrect or missing library dependency
I placed commons-dbcp jar file & mysql-java ... jar file into myappl\web-inf\lib folder
if u've any idea please do reply
You should do the things following:
1: Define a datasource in Tomcat[server.xml] or in Struts[struts-config.xml]
<Resource name="jdbc/ck" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>5000</value>
</parameter>
...
</ResourceParams>
2: Add a refference in the web app enviroment [web.xml].
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Wish to have a bit help for you!
mauves