2

i'm having some problems with tomcat 7 for configuring jdbc-pool : i`ve tried to follow this example: http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency

so i have:

conf/server.xml

 <GlobalNamingResources>
  <Resource type="javax.sql.DataSource"
            name="jdbc/DB"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/mydb"
            username="user"
            password="password"
/>
 </GlobalNamingResources>

conf/context.xml

<Context>
  <ResourceLink type="javax.sql.DataSource"
                name="jdbc/LocalDB"
                global="jdbc/DB"
/>
 <Context>

and when i try to do this:

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource datasource = (DataSource)envContext.lookup("jdbc/LocalDB");
Connection con = datasource.getConnection();

i keep getting this error:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
 at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:159)

pls help tnx

john
  • 21
  • 1
  • 2

3 Answers3

1

Check your context.xml file. Did you embed the <Context> within the existing <Context> of the default file?

Worst case scenario it to take this:

  <ResourceLink type="javax.sql.DataSource"
                name="jdbc/LocalDB"
                global="jdbc/DB"
/>

...and ensure it is within the already existing <Context> tag and get rid of the extraneous ones.

In general, I never recommend editing the server.xml or context.xml for the purpose of creating these resources. The Tomcat documentation will back me up on that to some extent:

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

See the tail end of the Introduction section. Typically I create context.xml.default files or even appname.xml files in the /conf/Catalina/localhost directory for such purposes. Abstracts the global configuration from the application specific configuration.

Corey S.
  • 2,379
  • 1
  • 18
  • 23
1

Typically I create context.xml.default files or even appname.xml files in the /conf/Catalina/localhost directory for such purposes.

It's the right way.

Luke
  • 11
  • 1
0

You can do the lookup in one line via java:comp/env/jdbc/DB, you don't need two lookups.

user207421
  • 990
  • 5
  • 16