package app.interceptor;
import java.sql.Connection;
import app.DbInitializer;
import com.blogger.tcuri.appserver.db.ConnectionLocator;
import com.blogger.tcuri.appserver.db.Transactional;
import com.blogger.tcuri.appserver.interceptor.Interceptor;
import com.blogger.tcuri.appserver.interceptor.InterceptorChain;
import com.blogger.tcuri.appserver.resolution.Resolution;
public class TxInterceptor implements Interceptor {
@Override
public Resolution execute(InterceptorChain chain) throws Exception {
if (chain.getAction().getClass().getAnnotation(Transactional.class) == null) {
return chain.execute();
}
Connection conn = DbInitializer.getConnection();
ConnectionLocator.set(conn);
Resolution resolution = null;
try {
resolution = chain.execute();
conn.commit();
} catch (Exception e) {
conn.rollback();
throw e;
} finally {
ConnectionLocator.get().close();
}
return resolution;
}
}