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

Tuesday, January 12, 2010

Netbeans and Tomcat: Shared Memory Realm problems when Tomcat is started in Debug Mode

Tomcat and Netbeans don't play nice. Everything starts out well, but the situation goes downhill every time the application is deployed, usually necessitating killing of Tomcat's JVM after a few deploys.

Finally I ran into a show stopper. It always goes away, yet I don't know what action causes it to go away, and then it comes back again later. The exact error was:

ERROR: transport error 202: failed to accept shared memory connection: stream closed

It only happens when Tomcat is started via Netbeans in debug mode. Sometimes there would be no error but no breakpoints were ever triggered.

How to fix it? Don't use a shared memory realm for debugging. The Netbeans and Tomcat JVM need some way to communicate during debugging and Netbeans provides two methods.

Go to services. Expand Servers and right click on Tomcat. Choose properties and select the startup tab. Change the radio button from Shared Memory Name to Socket Port. Kill Tomcat and start it in debug mode. Voila.

Tuesday, September 08, 2009

Quickly Tuning Tomcat JVM Options

Tomcat runs decently with default settings, but it's performance can be improved by tuning the CATALINA_OPTS string in Tomcat's startup file.

The parameters you'll want to play with most are -Xms and -Xmx which set the Java memory usage bounds. These can be tuned by starting the server and observing whether the Tomcat Java processes cause the server to access swap space. Tune –Xms and –Xmx to maximize the memory given to Tomcat without using swap space.

The -Dserver options hints to Tomcat that the machine is a server that won't be used as a desktop machine.

--XX:PermSize sets the size of the perm gen heap. I have found that this heap reaches some steady state. If the size is too small, when JSPs compile you'll get a PermGen space error. 100M-120M has worked well for the various applications I have worked on.

There are far more in depth tuning resources available if you Google around, but the above should get your started.

Here is a sample Catalina Opts string:

CATALINA_OPTS="-Dserver -Xms2048M -Xmx2048M -XX:PermSize=100M -XX:MaxGCPauseMillis=1000 -XX:GCTimeRatio=8 -DXlp -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:CMSIncrementalDutyCycleMin=0 -XX:CMSIncrementalDutyCycle=25 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1612 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.snmp.port=16666 -Dcom.sun.management.snmp.acl=true"

Friday, April 10, 2009

Tomcat's Catalina base and Catalina home

Sometimes you see references to tomcat/catalina home and tomcat/catalina base.  What's the difference.  The answer comes from Alessandro A. Garbagnati who writes:

"The first properties (catalina.home) points to the location of the common information, while the other property (catalina.base) points to the directory where all the instance specific information are held."

Basically, this way you can keep one copy of the Tomcat binaries yet use a different configuration by changing the directory to which base points (this directory must contain conf, logs, webapps, work, and temp).

Monday, October 29, 2007

Tomcat/Oracle CLOB gotcha?

Just spent two days troubleshooting why CLOB inserts were failing on one Tomcat install. It came down to two arguments in the resource declaration of my database in Tomcat:

accessToUnderlyingConnectionAllowed="true"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"

The observed failure was that CLOB columns were set to NULL, even though the insert appeared to work. In one table, the CLOB column was declared as XMLTYPE NOT NULL, and inserts would fail with an XMLType error at "line 241". The line 241 the error mentioned was not in my code but in Oracle's.

Besides this, it was necessary to make sure commons-dbcp-1.2.2.jar and commons-pool-1.3.jar were in tomcat's common/lib directory, otherwise I got NoClassDefFound errors in catalinia.out.

Friday, July 07, 2006

Adding a Database Context in Tomcat 5.5.x

This seems to be a giant freakin secret. Here's how to make it work:

(don't use the admin app, it doesn't work)

Look up the context using the string "java:comp/env/jdbc/quickstart"


----- server.xml ----
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/quickstart" docBase="quickstart">
<Resource name="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/quickstart">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@10.112.73.50:1521:EPS</value>
</parameter>
<parameter>
<name>driverClassName</name><value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>scott</value>
</parameter>
<parameter>
<name>password</name>
<value>tiger</value>
</parameter>

<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
</Host>



------ web.xml ----
<resource-ref>
<description>Connection Pool</description>
<res-ref-name>jdbc/quickstart</res-ref-name>
<res-type>javax.sql.Datasource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Friday, December 02, 2005

Tomcat 4 Fails to Start after Removing an Application

Tomcat 4 fails to start. Last time, before you shut down, you removed an application. On the next boot, the *first* error in Catalina.out says:

Catalina.start: LifecycleException: Context startup failed due to previous errors

What has happened is that there is a context in conf/server.xml (likely declaring a database connection) that references the application that was removed. The solution is to remove the context or comment it out.

Tuesday, June 21, 2005

JasperReports with Tomcat

Installed JRE & JDK 1.4.2_08 on server and a Windows PC.

Unzipped iReports-0.5.0 to the hard drive.

Copied tools.jar from the JRE to iReports-0.5.0/lib

Set up Tomcat 4.1.29 as usual on the server. Edited Catalina.conf to use the right JDK by setting JAVA_HOME then exporting it. Also added -Djava.awt.headless=true to JAVA_OPTS.

I created an application called reports

Went to webapps/reports/WEB-INF/lib

Copied the following files from iReports/lib

bsh-1.3.0.jar
commons-collections.jar
commons-logging-api.jar
itext-1.02b.jar
commons-beanutils.jar
commons-digester.jar
commons-logging.jar
jasperreports-0.6.7.jartools.jar

Looking at an old install, I also copied oracle.jar. Not sure where this came from but I assume it was needed to access our Oracle DB.

So now I create an index.html in /reports that has a link to /jsp/myreport.jsp

reports/jsp/myreport.jsp looks like this:

<%@page import="javax.sql.DataSource" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.CallableStatement" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="java.util.Date" %>
<%@page import="javax.naming.InitialContext" %>
<%@page import="java.util.ArrayList" %>
<%@page import="java.lang.Integer" %>


<%@ page errorPage="error.jsp" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>

<% InitialContext context = new InitialContext(); DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/test/testDB"); Connection connection = dataSource.getConnection(); File reportFile = new File(application.getRealPath("/reports/JasperTest.jasper")); JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); Map parameters = new HashMap(); parameters.put("ReportTitle", "Address Report"); parameters.put("BaseDir", reportFile.getParentFile()); Integer userpatientid = new Integer(25394); parameters.put("PatientId", userpatientid); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, connection ); JRHtmlExporter exporter = new JRHtmlExporter(); StringBuffer sbuffer = new StringBuffer(); Map imagesMap = new HashMap(); session.setAttribute("IMAGES_MAP", imagesMap);
// for PDF output, use exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
// I know I know it says XLS, but it works! Otherwise there is whitespace between rows
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image.jsp?image="); exporter.exportReport(); %>



Restart Tomcat and it works!

Friday, April 15, 2005

Adding Security to Tomcat Web Apps

  • Edit $CATALINA_HOME/conf/tomcat-users.xml
  • Make sure the user you want to use is there, with the role you want for your webapp
  • Now edit your application's web.xml, which is located in $CATALINA_HOME/webapps/YOURAPPLICATIONDIRECTORY/WEB-INF/web.xml
  • Add the following (I usually use just /* for url-pattern but you can get more specific):



<!-- Define a Security Constraint on this Application -->


<security-constraint>


<web-resource-collection>


<web-resource-name>NAME OF YOUR APP GOES HERE</web-resource-name>


<url-pattern>/*</url-pattern>


<url-pattern>/html/*</url-pattern>


<url-pattern>/list</url-pattern>


</web-resource-collection>


<auth-constraint>


<role-name>ROLE FROM TOMCAT-USERS.XML</role-name>


</auth-constraint>


</security-constraint>

 


<!-- Define the Login Configuration for this Application -->


<login-config>


<auth-method>BASIC</auth-method>


<realm-name>YOUR REALM NAME GOES HERE, JUST MAKE ONE UP</realm-name>


</login-config>

 


<!-- Security roles referenced by this web application -->


<security-role>


<description>


The role that is required to log in to this application


</description>


<role-name>ROLE FROM TOMCAT-USERS.XML</role-name>


</security-role>



That's it...

Labels

Blog Archive

Contributors