Friday, March 15, 2019

An interesting issue about "SQLRecoverableException: I/O Exception: Connection reset"

I developed a Java application (Spring boot) to pump the data from Oracle database to Mongo DB, to improve the performance, I use 8 processes to pump at the same time.
It works perfect on my local (Windows 10), it only takes 2.5 mins to pump 1 million records from remote Oracle to my local mongo. However, when I tried to run the application from the Linux box, it becomes very slow and after several minutes, I can see the exception "SQLRecoverableException: I/O Exception: Connection reset".

Oracle JDBC driver/JDK1.8 has a bug to generate the random number on some Linux (say Redhat), if the below command cannot return immediately then the issue will happen:
                 head -n 1 /dev/random
The fix is to set the property or
  1. Open the $JAVA_HOME/jre/lib/security/java.security file in a text editor.
  2. Change the line:
  3. securerandom.source=file:/dev/random
    to read:
    securerandom.source=file:/dev/urandom


http://www.usn-it.de/index.php/2009/02/20/oracle-11g-jdbc-driver-hangs-blocked-by-devrandom-entropy-pool-empty/
https://docs.oracle.com/cd/E13209_01/wlcp/wlss30/configwlss/jvmrand.html
https://stackoverflow.com/questions/6110395/sqlrecoverableexception-i-o-exception-connection-reset
https://community.oracle.com/thread/943911


Thursday, March 7, 2019

Spring boot note

1. Log
The default log messages will print to the console window. By default, “INFO”, “ERROR” and “WARN” log messages will print in the log file.
If you have to enable the debug level log, add the debug flag on starting your application using the command shown below −
java –jar demo.jar --debug
You can also add the debug mode to your application.properties file as shown here −
debug = true

By default, all logs will print on the console window and not in the files. 
You can specify the own log file name using the property shown below −
logging.file = /var/tmp/mylog.log

The code given below shows how to add the slf4j logger in Spring Boot main class file.
private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);
logger.info("this is a info message");
2. Exceptions
he @ControllerAdvice is an annotation, to handle the exceptions globally.
@ControllerAdvice
public class ProductExceptionController {
   @ExceptionHandler(value = ProductNotfoundException.class)
   public ResponseEntity<Object> exception(ProductNotfoundException exception) {
      return new ResponseEntity<>("Product not found", HttpStatus.NOT_FOUND);
   }
}
3. Rest Template
Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods.

4. Scheduling
The @EnableScheduling annotation is used to enable the scheduler for your application. 
The @Scheduled annotation is used to trigger the scheduler for a specific time period.
@Scheduled(cron = "0 * 9 * * ?")
The following is a sample code that shows how to execute the task every minute starting at 9:00 AM and ending at 9:59 AM, every day
package com.tutorialspoint.demo.scheduler;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Scheduler {
   @Scheduled(cron = "0 * 9 * * ?")
   public void cronJobSch() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
      Date now = new Date();
      String strDate = sdf.format(now);
      System.out.println("Java cron job expression:: " + strDate);
   }
}
5. Actuator
Spring Boot Actuator provides secured endpoints for monitoring and managing your Spring Boot application. 
In the application.properties file, we need to disable the security for actuator endpoints.
management.security.enabled = false

/metricsTo view the application metrics such as memory used, memory free, threads, classes, system uptime etc.
6. Oauth2 with JWT