Archive | java RSS for this section

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server

06:47:59,966 WARN http-8880-1 spi.SqlExceptionHelper:143 – SQL Error: 0, SQLState: 08003
06:47:59,986 ERROR http-8880-1 spi.SqlExceptionHelper:144 – No operations allowed after connection closed.
SEVERE: >>org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.JDBCConnectionException: No operations allowed after connection closed.; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: No operations allowed after connection closed.
>>javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: No operations allowed after connection closed.
>>org.hibernate.exception.JDBCConnectionException: No operations allowed after connection closed.
>>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
>>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 127,349,486 milliseconds ago. The last packet sent successfully to the server was 127,349,487 milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.
>>java.net.SocketException: Broken pipe
>> at java.net.SocketOutputStream.socketWrite0(Native Method)
>> at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)

Reason:

DB default connection idle timeout = 8hrs

If Application idle with no DB transaction before the timeout, DB connection will be dropped.

Solution:

1. Create own connection pool, and keep alive checking

2. Append autoconnect option at end of DB connection string,

eg. <property name=”hibernate.connection.url” value=”jdbc:mysql://127.0.0.1/db?autoReconnect=true” /

* not recommended by MYSQL (ref: http://pages.citebite.com/p4x3a0r8pmhm)

3. use c3p0 connection provider_class

<property name=”hibernate.connection.provider_class” value=”org.hibernate.connection.C3P0ConnectionProvider”/>
<property name=”hibernate.c3p0.min_size” value=”5″/>
<property name=”hibernate.c3p0.max_size” value=”20″/>
<property name=”hibernate.c3p0.timeout” value=”1800″/>
<property name=”hibernate.c3p0.max_statements” value=”50″/>
<property name=”hibernate.c3p0.testConnectionOnCheckout” value=”true”/>

(ref: https://forum.hibernate.org/viewtopic.php?t=991039

http://blog.csdn.net/nethibernate/article/details/6658855)

* C3P0ConnectionProvider class locate in separate jar

<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>3.6.3.Final</version>
		</dependency>

integrated weblogic cant create domain in Jdeveloper 11g release 2

platform: windows 8 64bit

jdk version: jdk6_34

Log:

wlst >
wlst > Failed to get environment, environ will be empty: (0, ‘Failed to execute command ([\’sh\’, \’-c\’, \’env\’]): java.io.IOException: Cannot run program “sh”: CreateProcess error=2, The system cannot find the file specified’)
wlst > Error: ADRS_DOMAIN_PASSWORD environment variable not set.

Reason: weblogic script wrongly identify OS platform, and use unix script which causing the problem.

Solution: patch jython-modules.jar in weblogic @ Middleware\wlserver_10.3\common\wlst\modules

step:

1. extract the jar

2. add new OS name at line 139 of  file:  javashell.py  inside [lib]

eg. _osTypeMap = (
( “nt”, ( ‘nt’, ‘Windows NT’, ‘Windows NT 4.0’, ‘WindowsNT’,
‘Windows 2000’, ‘Windows 2003’, ‘Windows XP’, ‘Windows CE’,
‘Windows Vista’, ‘Windows Server 2008’, ‘Windows 7’, ‘Windows 8’, ‘Windows_NT’ )),

reference :

http://www.oracle.com/technetwork/developer-tools/jdev/knownissues-097654.html#dep1

http://unversioned.blogspot.com/2012/12/jdevelopers-integrated-weblogic-on.html

add spring roo addon to support soft delete in entity

  1. svn checkout https://subversion.finalconcept.com.au/final-concept-spring-roo-behaviours/trunk/roo-addon-behaviours ./roo-addon-behaviours
    cd ./roo-addon-behaviours
    mvn install

Roo Commands

  • behaviours soft-delete add –type <JavaType>
  • behaviours soft-delete all

ref: https://redmine.finalconcept.com.au/projects/final-concept-spring-roo-behaviours/wiki/Installation

active record vs DAO in persistence layer

 main argument against using DAO: the fact that it requires a significant amount of repetitive source code to be produced for no immediate advantage over using JDBC or EJB directly. For many developers, this disadvantage is good enough reason to ignore the long-term benefits of using a framework-neutral API, especially where there are strict project deadlines. Without the code generation advantages of FireStorm/DAO, it is not easy to justify to a project manager or project sponsor the time and cost of manually writing DAO design pattern code, regardless of any future benefits they may see.

from: http://www.codefutures.com/products/firestorm/benefits/

Active record claim to be simple, which it encapsulate persistence into its object, and may implement with Rapid development tool like Spring Roo.

javax.xml.transform.TransformerException: java.io.FileNotFoundException

Writing xml to file while using java Transformer, having error below:

javax.xml.transform.TransformerException: java.io.FileNotFoundException

Environment: Ubuntu

Reason: Not sure.

I’d tested in both Maven tomcat and Standalone Tomcat 7, only occurred in Standalone Tomcat 7.

Sample code >

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, JRReportStyle.DEFAULT_ENCODING);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(anOutputFile);
transformer.transform(source, result);

Solution:

Change to use URI – getPath instead of just passing File object into StreamResult.

eg. StreamResult result = new StreamResult(anOutputFile.toURI.getPath());

‘report design not valid, field not found’ while generating Group Report using Dynamic Jasper.

While generating Group report using dynamicJasper 3.2.2 , it throw below error:

Report design not valid :
1. Field not found : customers_contactLastName
2. Field not found : customers_contactLastName

Cause:

Group criteria column or group variables not define in DRB(Dynamic Jasper builder) column list.

Solution:

Add Group criteria column and all group variables in DRB column list.

eg.

FastReportBuilder drb = new FastReportBuilder();
AbstractColumn critieriaCol = ColumnBuilder.getNew();
AbstractColumn display1 = ColumnBuilder.getNew();
drb.addColumn(display1);
drb.addColumn(criteriaCol);
GroupBuilder gb = new GroupBuilder(“group1”);
gb.setCriteriaColumn((PropertyColumn) criteriaCol);
DJGroup djGroup = gb.build();
drb.addGroup(djGroup);

How to conditional process action in JSF?

If validation failed, it should have a way to decide what action to do:

@Common JSF

<a4j:commandButton ... oncomplete="if (#{!facesContext.validationFailed}) $('#dialog_id').dialog('close')" />

@Primefaces

<p:commandButton value="submit" oncomplete="if (args &amp;&amp; !args.validationFailed) dialog.show()" process="@this"/>

How to align a component to right corner of JSF page?

solution:

1. include component in html table tag with align to right:

<table valign=”top” width=”100%”>
<tr>
<td align=”right”> <place component here> </td>
</tr>
</table>
2. use style in jsf component

put style=”float: right;” in component

eg. <h:outputText value=”test” style=”float: right;”/>

How to sort java collection?

Sort by specific element:

case1: sort by file name

Collection => List<File> fileList = new ArrayList<File>();

(ref: number of IO comparator can be used in apache commons)

import org.apache.commons.io.comparator.NameFileComparator;

Collections.sort(fileList, NameFileComparator.NAME_COMPARATOR);

ref:

http://www.java2s.com/Code/Java/Apache-Common/ComparatorExampleForUserDefinedClass.htm