Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Saturday, October 16, 2010

Remote Debug Web Start, Tomcat and Standalone Application

Remote debug web start is not as easy as applet.  Same setting for applet doesn't work at all.  Finally, I managed to make it work with an alternative solution.  Instead of launching the web start from a web site, I downloaded the JNLP file and launched it from command line.

My environment: Windows 7 64-bit, JDK 6 Update 14, MyEclipse for Spring.
Here is what I did:
  1. No need to setup Runtime Parameters in Java Control Panel this time.
  2. I used port 8200 for remote debug.
  3. server=y must be used
  4. suspend can be y or n.  If suspend=n, then Web Start will launch right away.  But if suspend=y, Web Start will not launch until a debugger is attached to port 8200.
  5. In Eclipse/MyEclipse, Debug Configuration must use Socket Attach as Connection Type and use port 8200.

Remote debug Tomcat or other standalone application is very similar to Web Start. However, the runtime parameters have to be added to the application's start script. And in this case, suspend is better to set to n so that the application will not wait for a debugger to attach. Since this type of application is most likely deployed to a remote server, Host in Debug Configuration also needs to match the corresponding domain.

Wednesday, January 6, 2010

Tomcat + JNDI DataSource + Spring

One tedious thing to do in project development is to keep track of configurations for different environment.  What happens often in a release is a wrong configuration causing the whole project to be rebuilt.  DataSource is one of the most common configuration.

And when Tech Op suggested to move configuration from project to them, I first started with setting up JNDI DataSource in Tomcat.

On Tomcat side, there are two places need to be configured:
1. Add the JDBC jar to Tomcat's library, for Tomcat 5.5, in common/lib directory.
2. Add the DataSource as a Resource to server.xml under GlobalNamingResources:
<Resource name="myApp" auth="Container"
    type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@<oracle db connection string>"
    username="<user name>" password="<password>"
    maxActive="10" maxIdle="5"
    maxWait="60000" removeAbandoned="true"
    removeAbandonedTimeout="60" logAbandoned="true"/>
Note: removeAbandoned=”true”, removeAbandonedTimeout=”60” and logAbandoned=”true” is Tomcat’s recommended solution to prevent dB connection pool leaks.


On the project side, there are also two places need to be configured:
1. In the web application's root directory, create a META-INF directory if not exists.  And add a context.xml to META-INF with following content:
<Context reloadable="true" >
    <ResourceLink name="jdbc/myApp" global="myApp" type="java.lang.Integer" />
</Context>
context.xml will be copied over to conf/Catalina/localhost with the the project's context name as file name if the file doesn't exist.  So for the first time deployment, it is necessary to check the conf/Catalina/localhost directory and delete the context file if exists.

2. In Spring configuration:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/myApp"/>
</bean>
An alternative configuration with jee schema:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
    ...
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/myApp"/>
</beans>