Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Eclipse, Tomcat, MySQL DataSource Name [java.naming.InitialContext] is not bound in this Context

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Environment:
MySql: 5.6.17 and using mysql-connector-java-5.1.33-bin.jar
Tomcat: 7.0.56
Java: 6
Eclipse: Eclipse Java EE IDE for Web Developers Version: Indigo Service Release 2
Mac OS X: 10.9.5

When I create a DataSource in Eclipse, I can successfully ping the database. However, when I try to use that DataSource programmatically, I get the attached StackTrace ( not allowed to attach StackTrace as it is in a .txt file). I will include a few lines here:

javax.naming.NameNotFoundException: Name [java.naming.InitialContext] is not bound in this Context. Unable to find [java.naming.InitialContext].
at org.apache.naming.NamingContext.list(NamingContext.java:370)
at org.apache.naming.NamingContext.list(NamingContext.java:394)
at org.apache.naming.SelectorContext.list(SelectorContext.java:352)
at javax.naming.InitialContext.list(InitialContext.java:436)
at com.schwartz.dao.MySql_Utils.MySqlConn(MySql_Utils.java:20)
at com.schwartz.customerProject.status.V1_status.returnDatabaseStatus(V1_status.java:38)

I have created and used a DataSource in Java code; that worked using the instructions at http://theopentutorials.com/examples/java-ee/servlet/servlet-jndi-datasource-in-tomcat/.

Do I need to modify the context.xml to use this DataSource created with the wizard? I added a resource-ref to the web.xml although the tutorial I was following did not indicate that was necessary.

This is the tutorial I used to create the DataSource with the wizard provided by Eclipse:
http://theopentutorials.com/tutorials/eclipse/dtp/eclipse-dtp-configure-mysql-datasource-data-source-explorer/

---------------------------------------------------------------------------------------------------

Partial web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.schwartz.customerProject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/customers/*</url-pattern>
</servlet-mapping>

<resource-ref>
<description>DB connection</description>
<res-ref-name>jdbc/customers</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

----------------------------------------------------------------------------
/WEB-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The contents of this file will be loaded for each web application -->

<Context crossContext="true">

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

<Resource name="jdbc/customers" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root"
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/customers"/>

</Context>

Thank you for your time.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your jdbc connection url, missing the database port (default to 3306)


 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying that now. Thanks. Attached screen shot shows the url correct when created by the wizard. When I test the connection in Eclipse when creating the DataSource, the ping works. This seems to be the problem when accessing the same DataSource in code:

javax.naming.NameNotFoundException: Name [java.naming.InitialContext] is not bound in this Context. Unable to find [java.naming.InitialContext].


Same result after modifying the context.xml; must be something else going on. I did stop and restart the server after adding the port.


Modified context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- The contents of this file will be loaded for each web application -->

<Context crossContext="true">

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

<Resource name="jdbc/customers" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root"
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/customers"/>


</Context>
Screen-Shot-2014-11-01-at-2.35.51-AM.png
[Thumbnail for Screen-Shot-2014-11-01-at-2.35.51-AM.png]
reply
    Bookmark Topic Watch Topic
  • New Topic