MySQL database connection fails for persistence in web application

I am having trouble with my MySQL persistence setup and need help. My environment includes Ubuntu 10.10, MySQL 5.1.49, Tomcat 6.0.28, and MySQL Connector/J 5.1.15. When I try to save forms, I get a naming context error.

The error message shows: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

I have configured the datasource in my server configuration file:

<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
          initialSize="2" maxActive="8" maxIdle="15" maxWait="25000"
          driverClassName="com.mysql.jdbc.Driver"
          poolPreparedStatements="true"
          username="dbuser"
          password="dbpass"
          url="jdbc:mysql://localhost:3306/myapp"/>

And my application properties:

<property as="xs:string" name="database.persistence.provider" value="mysql"/>
<property as="xs:string" name="database.mysql.datasource" value="mysql"/>

The database shows successful connections when the server starts, so I think my credentials are correct. But forms still won’t save and I keep getting the same JNDI lookup error. Has anyone encountered this issue before?

Had the exact same issue migrating an old app to a new server. Your problem is a mismatch between the JNDI resource name and your lookup reference. Server.xml defines it as “jdbc/mysql” but your app properties just use “mysql” - no jdbc prefix. That’s why the naming context fails at runtime even though the initial DB connection test works. Fix it by either updating your app properties to “jdbc/mysql” or changing the Resource name in server.xml to just “mysql”. Also check that your context.xml has the right ResourceLink if you’re using one - that gets missed a lot during deployment.

This naming exception happens because JNDI can’t find your datasource at runtime. I’ve seen this before - usually it’s a missing or wrong resource reference in web.xml. Your server.xml looks fine, but you need to add a resource-ref entry in web.xml that matches your datasource name. Try <resource-ref><res-ref-name>jdbc/mysql</res-ref-name><res-type>javax.sql.DataSource</res-type></resource-ref>. Your app can’t bind to the container resource without this reference. Also check that your JNDI lookup uses the right context - should be java:comp/env/jdbc/mysql instead of just jdbc/mysql. Those connection messages you’re seeing? That’s just the container starting up the pool, not your app actually using it.

Your JNDI naming issue is fixable, but there’s a cleaner approach to this persistence problem.

I’ve hit similar MySQL connection headaches in production. Even after fixing the naming context, you’ll likely face connection pool timeouts, deployment issues, or config drift between environments.

What worked for me: move the database logic to an automation platform. Instead of wrestling with JNDI lookups and Tomcat configs, I set up form processing to trigger workflows that handle MySQL operations externally.

This kills the server config complexity entirely. Your web app just sends form data to the automation workflow, which connects to MySQL directly and handles persistence. No JNDI context errors, no connection pool management, and easy replication across environments.

Workflows can also handle data validation, error handling, and complex business logic that’d normally clutter your app code. Plus you get built-in logging and monitoring for database operations.

I’ve been using Latenode for this exact pattern - solved all those config nightmares. Check it out: https://latenode.com

NameNotFoundException usually means your container config doesn’t match what your code’s actually looking up. I’ve debugged this tons of times - most people forget to check what their app is requesting vs what’s configured. Your server.xml looks fine, so the problem’s likely in your persistence layer’s JNDI lookup. Since the database connects at startup, the resource is being created properly. Add some debug code to print the exact JNDI name your app’s trying to resolve. Frameworks love to prepend context paths or mess with lookup strings behind the scenes. Also double-check your datasource property matches what your persistence provider wants - some frameworks need specific naming that doesn’t match the actual JNDI resource name.

Check your Tomcat logs for details on the JNDI binding failure. I hit the same issue - the resource wasn’t loading properly during webapp startup. Move your Resource definition from server.xml to your webapp’s context.xml. Tomcat sometimes struggles with global resources for individual apps.

Check your context path config - this got me once. Your datasource might be loading at the wrong scope. Add debug logging to your JNDI lookup to see exactly what context it’s searching. Also make sure your MySQL connector JAR is in Tomcat’s lib directory, not WEB-INF/lib. I wasted hours on the same issue only to find the driver was loading at webapp level instead of container level, making the JNDI resource fail silently. Do a quick JNDI context dump in your startup code to list all available resources and confirm your datasource is actually there.