Java

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Struggling with basic database connection

    8 answers - 2975 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I am struggling to figure out why my Tomcat installation is failing to allow
    me to connect my java web application to a database. I am looking for ideas
    as to how to find out what is wrong. I have (what I think) is the same setup
    on another machine and it works fine. The only difference between these is
    that the working one in tomcat 5.0 and this machine is tomcat 5.5 (although I
    am just about and try with a 5.0 setup).
    I am trying with this simple piece of code - plucked from the tomcat docs.
    Context initCtx;
    try {
    initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/akcmoney");
    Connection conn = ds.getConnection();
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    and with debug, I can see that initCtX, envCtx and ds all are assigned non
    null values, but the ds.getConnection() results in the following.
    Cannot create JDBC
    driver of class '' for connect URL 'null'
    I have the postgresql jdbc driver jar in $CATALINA.BASE/common/lib
    Server.xml contains
    <Context docBase="akcmoney" path="/akcmoney" reloadable="true"
    source="">
    <Resource auth="Container" name="jdbc/akcmoney"
    type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/akcmoney">
    <parameter>
    <name>factory</name>
    <value></value>
    </parameter>
    <parameter>
    <name>driverClassName</name>
    <value>org.postgresql.Driver</value>
    </parameter>
    <parameter>
    <name>url</name>
    <value></value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>tomcat4</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value>xxxxxxx</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>20</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>10</value>
    </parameter>
    <parameter>
    <name>maxWait</name>
    <value>-1</value>
    </parameter>
    </ResourceParams
    </Context>
    my WEB-INF/web.xml contains this reference.
    <resource-ref>
    <description>AKCMoney Database</description>
    <res-ref-name>jdbc/akcmoney</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    Anyone any ideas where I am going wrong, or how to look at the contents of the
    nitCtX, envCtx and ds variables in the above code snippet to understand how
    far I have got
  • No.1 | | 491 bytes | |

    Hello,

    there is a difference between how resources are defined in 5.0 and
    5.5. In 5.0 you could provide your resource definitions right in the
    server.xml, but in 5.5 you need to create a separate context.xml file
    ($CATALINA_HME/conf/Catalina/<hostname>/context.xml) and put resource
    definitions there.
    See this link:

    I also had this problem when migrating from 5.0 to 5.5. After I
    defined a separate context.xml it started to work again.

    Regards,
  • No.2 | | 1080 bytes | |

    Saturday 23 September 2006 09:57, Andrew Stepanenko wrote:
    Hello,

    there is a difference between how resources are defined in 5.0 and
    5.5. In 5.0 you could provide your resource definitions right in the
    server.xml, but in 5.5 you need to create a separate context.xml file
    ($CATALINA_HME/conf/Catalina/<hostname>/context.xml) and put resource
    definitions there.
    See this link:

    I also had this problem when migrating from 5.0 to 5.5. After I
    defined a separate context.xml it started to work again.

    The docs seem very vague on this issue, but it does imply that I could still
    use $CATALINA_HME/conf/server.xml

    question that isn't answered in the docs is what the root element should
    be - the best seems to be the example for accessing a database, and even
    there it puts <Context >

    I tried with context.xml in the directory you mentioned and it said it
    couldn't find the webapp "context", so I renamed it to my application and
    tried again. But it didn't work.

    So still stumped
  • No.3 | | 2123 bytes | |

    Hello,

    looks like the docs I gave a link to describe the old approach, sorry.
    Here is what I have in my context.xml:

    <!-- The contents of this file will be loaded for each web application
    <Context path="" docBase="/"
    debug="1" reloadable="true">

    <!-- Default set of monitored resources
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource name="jdbc/unfasHB" auth="Container" scope="Shareable"
    type="javax.sql.DataSource"
    factory=""
    url=";characterEncoding=UTF-8&characterSetResults=UTF-8"
    driverClassName="com.mysql.jdbc.Driver"
    username="andrews"
    password="xxx"
    maxIdle="5"
    maxActive="50"
    autoReconnect="true"
    />
    </Context>

    also, the format of <Resourcehas been changed. Now, all
    configuration data should be provided as attributes and not nested
    tags.

    Regards,
    Andrew Stepanenko,
    http://unf.tane.edu.ua

    9/23/06, Alan Chandler <alan (AT) chandlerfamily (DOT) org.ukwrote:
    Saturday 23 September 2006 09:57, Andrew Stepanenko wrote:
    Hello,

    there is a difference between how resources are defined in 5.0 and
    5.5. In 5.0 you could provide your resource definitions right in the
    server.xml, but in 5.5 you need to create a separate context.xml file
    ($CATALINA_HME/conf/Catalina/<hostname>/context.xml) and put resource
    definitions there.
    See this link:

    I also had this problem when migrating from 5.0 to 5.5. After I
    defined a separate context.xml it started to work again.

    The docs seem very vague on this issue, but it does imply that I could still
    use $CATALINA_HME/conf/server.xml

    question that isn't answered in the docs is what the root element should
    be - the best seems to be the example for accessing a database, and even
    there it puts <Context >

    I tried with context.xml in the directory you mentioned and it said it
    couldn't find the webapp "context", so I renamed it to my application and
    tried again. But it didn't work.

    So still stumped
  • No.4 | | 2741 bytes | |

    There are two pieces of information about the Context file while deploying
    your application as a WAR that the Tomcat documentation fails to make
    absolutely clear:

    1) the Context file name must be "context.xml"; do not attempt to name it
    anything else.
    2) put the file in the /WEB-INF folder of the WAR

    The file will be extracted and the name changed automatically by the Tomcat
    engine. than that, what Mr. Stepanenko gave you is relatively
    identical to what I have in my Context file.

    H

    9/23/06, Andrew Stepanenko <andrew.stepanenko (AT) gmail (DOT) comwrote:

    Hello,

    looks like the docs I gave a link to describe the old approach, sorry.
    Here is what I have in my context.xml:

    <!-- The contents of this file will be loaded for each web application
    <Context path="" docBase="/"
    debug="1" reloadable="true">

    <!-- Default set of monitored resources
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource name="jdbc/unfasHB" auth="Container" scope="Shareable"
    type="javax.sql.DataSource"
    factory=""

    url=";characterEncoding=UTF-8&characterSetResults=UTF-8"
    driverClassName="com.mysql.jdbc.Driver"
    username="andrews"
    password="xxx"
    maxIdle="5"
    maxActive="50"
    autoReconnect="true"
    />
    </Context>

    also, the format of <Resourcehas been changed. Now, all
    configuration data should be provided as attributes and not nested
    tags.

    Regards,
    Andrew Stepanenko,
    http://unf.tane.edu.ua

    9/23/06, Alan Chandler <alan (AT) chandlerfamily (DOT) org.ukwrote:
    Saturday 23 September 2006 09:57, Andrew Stepanenko wrote:
    Hello,

    there is a difference between how resources are defined in 5.0 and
    5.5. In 5.0 you could provide your resource definitions right in the
    server.xml, but in 5.5 you need to create a separate context.xml file
    ($CATALINA_HME/conf/Catalina/<hostname>/context.xml) and put resource
    definitions there.
    See this link:

    I also had this problem when migrating from 5.0 to 5.5. After I
    defined a separate context.xml it started to work again.

    The docs seem very vague on this issue, but it does imply that I could
    still
    use $CATALINA_HME/conf/server.xml

    question that isn't answered in the docs is what the root element
    should
    be - the best seems to be the example for accessing a database, and even
    there it puts <Context >

    I tried with context.xml in the directory you mentioned and it said it
    couldn't find the webapp "context", so I renamed it to my application
    and
    tried again. But it didn't work.

    So still stumped
  • No.5 | | 5136 bytes | |

    Correction: context.xml belongs in META-INF of the war archive.

    Henry McClain wrote:
    There are two pieces of information about the Context file while
    deploying
    your application as a WAR that the Tomcat documentation fails to make
    absolutely clear:

    1) the Context file name must be "context.xml"; do not attempt to name it
    anything else.
    2) put the file in the /WEB-INF folder of the WAR

    The file will be extracted and the name changed automatically by the
    Tomcat
    engine. than that, what Mr. Stepanenko gave you is relatively
    identical to what I have in my Context file.
    --
    H

    9/23/06, Andrew Stepanenko <andrew.stepanenko (AT) gmail (DOT) comwrote:
    >>

    >Hello,
    >>

    >looks like the docs I gave a link to describe the old approach, sorry.
    >Here is what I have in my context.xml:
    >>

    >
    ><!-- The contents of this file will be loaded for each web
    >application
    ><Context path="" docBase="/"
    >debug="1" reloadable="true">
    >>

    ><!-- Default set of monitored resources
    ><WatchedResource>WEB-INF/web.xml</WatchedResource>
    >>

    ><Resource name="jdbc/unfasHB" auth="Container" scope="Shareable"
    >type="javax.sql.DataSource"
    >factory=""
    >>

    >
    >url=";characterEncoding=UTF-8&characterSetResults=UTF-8"
    >>

    >driverClassName="com.mysql.jdbc.Driver"
    >username="andrews"
    >password="xxx"
    >maxIdle="5"
    >maxActive="50"
    >autoReconnect="true"
    >/>
    ></Context>
    >
    >>

    >also, the format of <Resourcehas been changed. Now, all
    >configuration data should be provided as attributes and not nested
    >tags.
    >>

    >Regards,
    >Andrew Stepanenko,
    >http://unf.tane.edu.ua
    >>

    >9/23/06, Alan Chandler <alan (AT) chandlerfamily (DOT) org.ukwrote:
    >Saturday 23 September 2006 09:57, Andrew Stepanenko wrote:
    >Hello,
    >>

    >there is a difference between how resources are defined in 5.0 and
    >5.5. In 5.0 you could provide your resource definitions right in the
    >server.xml, but in 5.5 you need to create a separate context.xml
    >file
    >($CATALINA_HME/conf/Catalina/<hostname>/context.xml) and put
    >resource
    >definitions there.
    >See this link:
    >
    >>

    >I also had this problem when migrating from 5.0 to 5.5. After I
    >defined a separate context.xml it started to work again.
    >>

    >The docs seem very vague on this issue, but it does imply that I
    >could
    >still
    >use $CATALINA_HME/conf/server.xml
    >>

    >question that isn't answered in the docs is what the root element
    >should
    >be - the best seems to be the example for accessing a database, and
    >even
    >there it puts <Context >
    >>

    >I tried with context.xml in the directory you mentioned and it said it
    >couldn't find the webapp "context", so I renamed it to my application
    >and
    >tried again. But it didn't work.
    >>

    >So still stumped
    >>

    >--
    >Alan Chandler
    >
    >>
    >>

    >
    >To start a new topic, e-mail: users (AT) tomcat (DOT) apache.org
    >To unsubscribe, e-mail: users-unsubscribe (AT) tomcat (DOT) apache.org
    >For additional commands, e-mail: users-help (AT) tomcat (DOT) apache.org
    >>
    >>
    >>
    >>

    >--
    >Andrew Stepanenko,
    >Ph.D. student,
    >Dept. of Information Computing Systems and Control,
    >Web:
    >Software engineer,
    >Ukrainian-Dutch Faculty of Economics and Management
    >Ternopil State Economic University
    >Shevchenko Street 9, 24-25
    >Ternopil, 46000 UKRAINE
    >Tel: +38 (0352) 43 52 41
    >fax: +38 (0352) 43 52 45
    >Web: http://unf.tane.edu.ua
    >>

    >
    >To start a new topic, e-mail: users (AT) tomcat (DOT) apache.org
    >To unsubscribe, e-mail: users-unsubscribe (AT) tomcat (DOT) apache.org
    >For additional commands, e-mail: users-help (AT) tomcat (DOT) apache.org
    >>
    >>

    >
    >


    To start a new topic, e-mail: users (AT) tomcat (DOT) apache.org
    To unsubscribe, e-mail: users-unsubscribe (AT) tomcat (DOT) apache.org
    For additional commands, e-mail: users-help (AT) tomcat (DOT) apache.org
  • No.6 | | 824 bytes | |

    Saturday 23 September 2006 15:23, David Smith wrote:
    Correction: context.xml belongs in META-INF of the war archive.

    K, I am begining to comprehend - and knowledge about the context.xml in the
    META-INF file for a WAR archive is good. I was looking for a way to wrap all
    the info about the application in one directory structure - for my version
    control.

    Does this work with Tomcat 5.0 - thats my current production environment?

    HWEVER, in the short term I am not working with WAR files - instead I am
    trying to deploy the application inside Eclipse. I tried creating a directory
    etc under eclipse's (probably WTP plugin's) server configuration, but it
    doesn't publish that to the conf directory inside it's CATALINA_HME.

    So still not sure how to proceed.
  • No.7 | | 1985 bytes | |

    you may very well be publishing to your own webapp context.xml
    i.e $
    Until you feel comfortable with the reliability of deployment in the meanwhile I would create your war by hand
    (and then let the IDE automate only after you have already established a reliable manual deployment environment)
    *Most IDE's* support the capability to export build command which will produce a build.xml you can work with
    this presupposes you have downloaded ANT and set ANT_HME to the location where you installed Ant
    M-
    This email message and any files transmitted with it contain confidential
    information intended only for the person(s) to whom this email message is
    addressed. If you have received this email message in error, please notify
    the sender immediately by telephone or email and destroy the original
    message without making a copy. Thank you.

    Message
    From: "Alan Chandler" <alan (AT) chandlerfamily (DOT) org.uk>
    To: <users (AT) tomcat (DOT) apache.org>
    Sent: Saturday, September 23, 2006 12:48 PM
    Subject: Re: Struggling with basic database connection

    Saturday 23 September 2006 15:23, David Smith wrote:

    >Correction: context.xml belongs in META-INF of the war archive.


    K, I am begining to comprehend - and knowledge about the context.xml in the
    META-INF file for a WAR archive is good. I was looking for a way to wrap all
    the info about the application in one directory structure - for my version
    control.

    Does this work with Tomcat 5.0 - thats my current production environment?

    HWEVER, in the short term I am not working with WAR files - instead I am
    trying to deploy the application inside Eclipse. I tried creating a directory
    etc under eclipse's (probably WTP plugin's) server configuration, but it
    doesn't publish that to the conf directory inside it's CATALINA_HME.

    So still not sure how to proceed.
  • No.8 | | 377 bytes | |

    Saturday 23 September 2006 15:23, David Smith wrote:
    Correction: context.xml belongs in META-INF of the war archive.

    I have just conducted experiments with Tomcat 5.0

    putting a context.xml file in META-INF doesn't appear to work

    I have not been having success with putting in conf/server.xml - but I have
    had success with putting the context in

Re: Struggling with basic database connection


max 4000 letters.
Your nickname that display:
In order to stop the spam: 8 + 7 =
QUESTION ON "Java"

EMSDN.COM