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>