public class WorklogServiceImpl implements WorklogService { private static volatile WorklogService instance = null; private static volatile WorklogService proxy = null; private WorklogServiceImpl(){ } public static WorklogService newInstance() { if(instance==null) { synchronized(WorklogServiceImpl.class) { if(instance==null) { instance = new WorklogServiceImpl(); } } } if(proxy==null) { synchronized(WorklogServiceImpl.class) { if(proxy==null) { Class<?> proxyClass = Proxy.getProxyClass(instance.getClass().getClassLoader(), instance.getClass().getInterfaces()); try { Constructor<?> proxyConstructor = proxyClass.getConstructor(new Class[] {InvocationHandler.class}); proxy = (WorklogService)proxyConstructor.newInstance(new Object[] { new InvocationHandler() { public Object invoke(Object proxy, Method method, Object args[]) throws Throwable { Object result = null; Session session = HibernateSessionFactory.getSession(); session.connection().setAutoCommit(false); Transaction t = session.beginTransaction(); try { t.begin(); result = method.invoke(instance, args); t.commit(); } catch(Exception e) { t.rollback(); session.close(); System.out.println("发生异常,事务撤销。"); e.printStackTrace(); throw e; } return result; } } } ); } catch(Exception e) { e.printStackTrace(); throw new RuntimeException("create WorklogServiceImpl proxy instance failed!"); } } } } return proxy; } }