Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tomcat/mysql configuration help?

 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm running 3 different servers, one machine - three different ports, for beta, gamma and production environments and would like to build to the beta environment and build WARs to promote to gamma and production. I think I have that under control.

Where I'm having issues is coming up with a configuration for the database. I want to run beta, gamma and production databses, for which I assume I'll run the same tables in different databases within the same mysql instance.

To that end I'd like to configure a resource at the server level that points to the correct db and has the same name across the three servers. This is where I get confused. The JNDI documentation says I should declare the resource in the context in server.xml, but 5.X tomcat doesn't declare contexts in server.xml and I'm building everything out of webapps/ROOT, so it doesn't seem to have any context file. The example for GlobalResources (which I've used to some success for authentication... except that some people had to log in multiple times for a single page and some didn't) seems to be for authentication. Can I use the JNDI example in the global resources section then add a resource link in the web.xml?

I'm really frustrated with this on an architecture level. It seems the Tomcat documents are contradictory and/or incomplete. I've yet to get a naming resource to work, I've been doing everything the old direct connect way... but that isn't as viable as I'm trying to make this into a mature dev environment.

Thanks,

Jimbus
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking that you'll get better coverage for this in the Tomcat forum, so moving there.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but 5.X tomcat doesn't declare contexts in server.xml


But you still can, and I still do just because I haven't had the time yet to figure out the new scheme -- especially when it comes to the JNDI naming.
 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright,

I have something working:

In $TOMCAT_HOME/conf there is a context.xml file that I added:

<Resource name="jdbc/apps" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="appadmin" password="XXXXXX" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/beta?autoReconnect=true"/>

In WEB-INF/web.xml:


<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/apps</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

For a test app:

<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>

<html><body>

<%
Context envContext = (Context)new InitialContext().lookup("java:comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/apps");
connection = ds.getConnection();
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM status");
while (rs.next()) {
%>
<li><%=rs.getString("owner")%>: <%=rs.getString("alert")%>
<%
}
rs.close();
%>
</body></html>

I copied context.xml to the gamma server's conf directory and changed the db in the url from beta to gamma, copied the test page and web.xml with no modifications and each got data from the correct place. I'm much happier now, the data URL is abstracted from the app.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possible solution is to create 3 seperate context.xmls pointing to your beta, gama and production databases. Place the context.xml in the META-INF directory of each of your 3(beta, gama, production) WAR files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic