Java Transaction API

Transaction: an exchange or interaction between people.

Native Support
https://en.wikipedia.org/wiki/Java_Transaction_API
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html

Third Party Support
http://narayana.io/index.html
https://www.atomikos.com/
https://github.com/bitronix/

Notes:
1. If we are doing credit card transaction, we need to do reverse swipe separately (We can write separate task and execute, but to be careful and need to handle, if we fail to perform reverse swipe).
2. If credit card transaction failed, we can’t do it again and again.
We need to understand and apply common sense on how to deal with it.

What is the Java equivalent for LINQ?

jOOQ: http://www.jooq.org
JINQ: http://www.jinq.org
JaQue: http://github.com/TrigerSoft/jaque
JaQu: http://www.h2database.com/html/jaqu.html
Linq4j: https://github.com/julianhyde/linq4j
Quaere: http://quaere.codehaus.org/
QueryDSL: http://www.querydsl.com
JSR-341: http://java.net/projects/el-spec/pages/CollectionOperations

Reference: http://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq

Thanks to Lukas Eder
http://stackoverflow.com/users/521799/lukas-eder

Ref: http://blog.jooq.org/2013/07/01/linq-and-java/
———————-
Notes:
1. These are tightly coupling with database and may cause brittleness down the line.
2. Performance fine tuning is tough.
3. Saves time and lines of code and increases productivity.
4. Better to write stored procs and use them.
5. C# LINQ looks good, but didn’t liked it.

-o-

Commons Validator

Many times Developers are tempted to write their own validations.
If using Java, better to use “Commons Validator”

https://commons.apache.org/proper/commons-validator/

Few Points:
1. Always check for null. Don’t assume that customer will provide data.
2. Always check for boundary values….i.e min/max/…etc
3. Better to associate Error Messages with Error Codes. Pretty much required for Automation.
4. Externalize Strings to support multi lingual support
5. If not doing anything, don’t catch exceptions.
6. Don’t swallow exceptions….preserve the stack trace
7. Don’t convert exception A to exception B
8. Use Sonar and fix the values
9. Define constants separately….if possible configurable in future.

-0-

Java Exception Handling / Performance

package com.learning;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Investigating performance issues behind exception handling / logging
 * 
 * @author Bhavani Polimetla
 * @since 06-May-2014
 */
public class ExceptionLoggingPerformance {

	private static Logger LOG = LoggerFactory.getLogger("ExceptionLoggingPerformance");

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		LOG.info("-----------------------------------------------");
		long start = System.nanoTime();
		test10();
		long end = System.nanoTime();
		long test10Time = end - start;
		
		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test11();
		end = System.nanoTime();
		long test11Time = end - start;
		
		
		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test1();
		end = System.nanoTime();
		long test1Time = end - start;
		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test2();
		end = System.nanoTime();
		long test2Time = end - start;

		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test3();
		end = System.nanoTime();
		long test3Time = end - start;

		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test4();
		end = System.nanoTime();
		long test4Time = end - start;

		
		LOG.info("-----------------------------------------------");
		start = System.nanoTime();
		test20();
		end = System.nanoTime();
		long test20Time = end - start;

		
		LOG.info("-----------------------------------------------");
		long totalTime = test1Time + test2Time + test4Time + test10Time+test11Time
				+ test3Time + test20Time;

		LOG.info("test 10 catch (Exception e) e.printStackTrace(); time: "
				+ test10Time * 100 / totalTime + "%");
		LOG.info("test 11 catch (Throwable e) e.printStackTrace(); time: "
				+ test11Time * 100 / totalTime + "%");

		LOG.info("test 1 catch (Exception e) LOG.error("test1", e); time: "
				+ test1Time * 100 / totalTime + "%");
		LOG.info("test 2  catch (Throwable e) LOG.error("test2", e); time: "
				+ test2Time * 100 / totalTime + "%");
		LOG.info("test 3  catch (Exception e) LOG.error(ExceptionUtils.getStackTrace(e)); time: "
				+ test3Time * 100 / totalTime + "%");

		LOG.info("test 4 catch (Throwable e) LOG.error(ExceptionUtils.getStackTrace(e)); time: "
				+ test4Time * 100 / totalTime + "%");
		
		LOG.info("test 20 catch (Exception e) LOG.error(e.getStackTrace().toString()); time: "
				+ test20Time * 100 / totalTime + "%");

	}
	
	public static void test10() {

		try {
			int j = 1 / 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void test11() {

		try {
			int j = 1 / 0;
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public static void test1() {

		try {
			int j = 1 / 0;
		} catch (Exception e) {
			LOG.error("test1", e);
		}
	}

	public static void test2() {

		try {
			int j = 1 / 0;
		} catch (Throwable e) {
			LOG.error("test2", e);
		}
	}

	public static void test3() {

		try {
			int j = 1 / 0;
		} catch (Exception e) {
			LOG.error(ExceptionUtils.getStackTrace(e));
		}
	}

	public static void test4() {

		try {
			int j = 1 / 0;
		} catch (Throwable e) {
			LOG.error(ExceptionUtils.getStackTrace(e));
		}
	}
	
	public static void test20() {

		try {
			int j = 1 / 0;
		} catch (Exception e) {
			LOG.error(e.getStackTrace().toString());
		}
	}

	
}

Result
test 10 catch (Exception e) e.printStackTrace(); time: 9%
test 11 catch (Throwable e) e.printStackTrace(); time: 2%
test 1 catch (Exception e) LOG.error(“test1”, e); time: 36%
test 2 catch (Throwable e) LOG.error(“test2”, e); time: 3%
test 3 catch (Exception e) LOG.error(ExceptionUtils.getStackTrace(e)); time: 41%
test 4 catch (Throwable e) LOG.error(ExceptionUtils.getStackTrace(e)); time: 4%
test 20 catch (Exception e) LOG.error(e.getStackTrace().toString()); time: 1%

Note:
http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html
Because performance is good, doesn’t mean that we can use catch(Throwable e).
Better not to handle Throwable, because these are for system to handle.
Also when we are not acting/checking the reason in exception block, no need to handle and swallow the important information.

Also ExceptionUtils.getStackTrace(e) had separate purpose.
No need to use this inside LOG statement to convert exception into single string.
-o-