23/12/2008

Some questions about Java

Collection
To know all about Collection interface, subinterfaces and implementation.
For example:
  • subinterface List, Set and Map
  • class ArrayList, HashSet, HasMap (what are the differences between each class?)
JEE
List of specifications (Webservices, annotations, JSF, JSP, Servlet, EJB, JMS, etc.)

Transation
There are times when you do not want one statement to take effect unless another one completes.
The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode.
A SQLException should call a rollback.

Exception
unchecked exception is a subclass or RuntimeException and Error (NullPointerException, ClassCastException, etc.)

Design patterns
Singleton, prototype, Decorator, Facade, Factory, Proxy, ...

25/05/2008

Struts internationalization i8n

Multilanguage web application
Only in presentation layer

Add several resource files in your package
com.mycompany.mypackage.MyApplication.properties (default)
com.mycompany.mypackage.MyApplication_fr.properties (French messages)
com.mycompany.mypackage.MyApplication_en.properties (English messages)

Add resource file reference in Struts
tag: message-resources, parameter="com.mycompany.mypackage.MyApplication"

Add locale in page
struts tag (open/close tag): html:html, locale="true"

13/01/2008

DBCP: SQL Error: 17410

Database connection pool
Sometimes when you put your application in Production, you can get an sql exception, database connection error
ERROR - JDBCExceptionReporter - No more data to read from socket (java.sql.SQLException: No more data to read from socket)
JDBCExceptionReporter - SQL Error: 17410, SQLState: null
JDBCExceptionReporter - SQL Error: 0, SQLState: null (at org.hibernate.jdbc.ConnectionManager.aggressiveRelease(ConnectionManager.java:400))

The origin of the problem is due to how you manage your datasource and the connection manager.
Usually, the issue not detected in Development, because the default configuration of you pool manager is right, but in Production, the default configuration can be weak.
If you meet theses kind of exception, it is because the pool does not clean "dead" connection. It can be easily customized in the configuration of the pool.

For example: with dbcp (apache.commons.dbcp)



<bean id="MyDatasource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="..." />
<property name="url" value="..." />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="defaultAutoCommit" value="true" />
<property name="maxActive" value="10" />
<property name="maxWait" value="120000" />
<property name="validationQuery" value="select 1 from dual" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!--property name="poolPreparedStatements" value="true" /--><!--leave it to false with Spring and Hibernate-->
<!--property name="maxOpenPreparedStatements" value="40" /-->
</bean>

31/12/2007

Eclipse memory

Create a shortcut. For example:

C:\eclipse\eclipse.exe –data C:\MyWorkspace –vmargs –Xms128m –Xmx256m

11/09/2007

Maven 2: useful command lines

Web application
Create a Blank web app (war)
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.abb.example -DartifactId=example

Struts 1
Create a Struts Blank web app (war)
mvn archetype:create -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts-archetype-blank -DarchetypeVersion=1.3.5 -DgroupId=com.abb.example -DpackageName=com.abb.example.test -DartifactId=app-test (error: deprecated goal with this version)

Struts 2
Create a Struts 2 tutorial web app (war)
mvn archetype:create -DgroupId=tutorial -DartifactId=tutorial -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.9-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository

WTP
Set the project as a web app in WTP (Eclipse Web Tool)
mvn clean eclipse:clean eclipse:eclipse -Dwtpversion=1.5
Add in pom.xml:
  maven-eclipse-plugin, configuration wtpversion = 1.5
and maybe:
  maven-compiler-plugin, 1.5 (in build plugins)


New projet
Create a java project (jar)
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

...

01/09/2007

Tomcat jar locking

Tomcat locks some jar files

Symptom
In Tomcat server, when I undeploy my web application, there are still some jar files locked by the server.

Solution
Use the following parameters: antiJARLocking and antiResourceLocking in the context configuration.
Example:
<!-- The contents of this file will be loaded for each web application -->
<Context antiJarLocking="true" antiResourceLocking="true">

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

</Context>


Link
Tomcat configuration context