Tuesday, September 30, 2014

MultiThreading and Exception

Class ThreadException {

TestKl0t1Example example = new TestKl0t1Example();
TestKl0t1Example.Criteria criteria = example.createCriteria();
criteria.andT1trnoEqualTo(new BigDecimal(dto.getTranNo()))

List<Object> dbResult = getCommonDao()
.selectByExampleDltmOff(example);


t1 = (TestKl0t1) dbResult.get(0);
List<TestKl0t1> results = new ArrayList<TestKl0t1>();
for (int i = 0; i < dbResult.size(); i++) {
results.add((TestKl0t1) dbResult.get(i));
}
ExecutorService executor = Executors.newFixedThreadPool(dbResult
.size());
List<Transactiompl> callingList = new ArrayList<ThreadException.Transactiompl>();
for (int i = 0; i < dbResult.size(); i++) {
Transactiompl localImpl = new Transactiompl(dbResult.get(i));
callingList.add(localImpl);
}
List<Future<TestKl0t1>> results1 = null;

try {

results1 = executor.invokeAll(callingList);

// executor.shutdown();
// while (!executor.isTerminated()) {
// executor=null;
// }

} catch (Exception e) {
executor = null;

}
executor = null;

}
Class Transactiompl extends RequestContextAwareCallable{
Transactiompl(Object readTestKl0t1) {
this.t1 = (TestKl0t1) readTestKl0t1;

}



public TestKl0t1 onCall() throws InterruptedException {
beginTransaction();





public abstract class RequestContextAwareCallable<V> implements Callable<V> {

private final RequestAttributes requestAttributes;
private Thread thread;

public RequestContextAwareCallable() {
this.requestAttributes = RequestContextHolder
.getRequestAttributes();
this.thread = Thread.currentThread();
}

public V call() throws Exception {
try {
RequestContextHolder.setRequestAttributes(requestAttributes);
return onCall();
} catch (Exception e) {
System.out.println("Oh boy, something broke!");
e.printStackTrace();
rollbackTransaction();
checkforcommit=false;
throw e;


}

finally {
if (Thread.currentThread() != thread) {
RequestContextHolder.resetRequestAttributes();
}

thread = null;
}
}

public abstract V onCall() throws Exception;
public void beginTransaction() {

DefaultTransactionDefition txDef = new DefaultTransactionDefition();
txDef.setPropagationBehavior(0);
txStatus = txManager.getTransaction(txDef);
}

public void commitTransaction() {
if (txStatus != null) {
if (!txStatus.isCompleted())
txManager.commit(txStatus);
} else {
System.out.println("error");
}
}

public void rollbackTransaction() {
if (txStatus != null) {
if (!txStatus.isCompleted())
txManager.rollback(txStatus);
} else {
System.out.println("rollbackcalled");
}
}
}

@Override
public void resetGlobalVariables() {
// TODO Auto-generated method stub

}

private PlatformTransactionManager txManager;

public void setTxManager(PlatformTransactionManager txManager) {
this.txManager = txManager;
}

public PlatformTransactionManager getTxManager() {
return txManager;
}

No comments:

Post a Comment