@Override public <T> T transactional(Supplier<T> beanSupplier) { TransactionProxyFactoryBean proxy = new TransactionProxyFactoryBean(); // Inject transaction manager here proxy.setTransactionManager(transactionManager); // Define which object instance is to be proxied (your bean) proxy.setTarget(beanSupplier.get()); // Programmatically setup transaction attributes Properties transactionAttributes = new Properties(); transactionAttributes.put("*", "PROPAGATION_REQUIRED"); proxy.setTransactionAttributes(transactionAttributes); // Finish FactoryBean setup proxy.afterPropertiesSet(); @SuppressWarnings("unchecked") T ret = (T) proxy.getObject(); return ret; } }
TransactionProxyFactoryBean proxy = new TransactionProxyFactoryBean();
/** * <p>create</p> * * @param ifaceId a {@link java.lang.Integer} object. * @param ifaceDao a {@link org.opennms.netmgt.dao.api.IpInterfaceDao} object. * @param transMgr a {@link org.springframework.transaction.PlatformTransactionManager} object. * @return a {@link org.opennms.netmgt.collection.api.CollectionAgentService} object. */ public static SnmpCollectionAgentService create(Integer ifaceId, final IpInterfaceDao ifaceDao, final PlatformTransactionManager transMgr) { SnmpCollectionAgentService agent = new DefaultSnmpCollectionAgentService(ifaceId, ifaceDao); TransactionProxyFactoryBean bean = new TransactionProxyFactoryBean(); bean.setTransactionManager(transMgr); bean.setTarget(agent); Properties props = new Properties(); props.put("*", "PROPAGATION_REQUIRED"); bean.setTransactionAttributes(props); bean.afterPropertiesSet(); return (SnmpCollectionAgentService) bean.getObject(); }
/** * <p>create</p> * * @param ifaceId a {@link java.lang.Integer} object. * @param ifaceDao a {@link org.opennms.netmgt.dao.api.IpInterfaceDao} object. * @param transMgr a {@link org.springframework.transaction.PlatformTransactionManager} object. * @return a {@link org.opennms.netmgt.collection.api.CollectionAgentService} object. */ public static CollectionAgentService create(Integer ifaceId, final IpInterfaceDao ifaceDao, final PlatformTransactionManager transMgr) { CollectionAgentService agent = new DefaultCollectionAgentService(ifaceId, ifaceDao); TransactionProxyFactoryBean bean = new TransactionProxyFactoryBean(); bean.setTransactionManager(transMgr); bean.setTarget(agent); Properties props = new Properties(); props.put("*", "PROPAGATION_REQUIRED"); bean.setTransactionAttributes(props); bean.afterPropertiesSet(); return (CollectionAgentService) bean.getObject(); }
/** * <p>create</p> * * @param ifaceId a {@link java.lang.Integer} object. * @param ifaceDao a {@link org.opennms.netmgt.dao.api.IpInterfaceDao} object. * @param transMgr a {@link org.springframework.transaction.PlatformTransactionManager} object. * @return a {@link org.opennms.netmgt.collection.api.CollectionAgentService} object. */ public static CollectionAgentService create(Integer ifaceId, final IpInterfaceDao ifaceDao, final PlatformTransactionManager transMgr) { CollectionAgentService agent = new DefaultCollectionAgentService(ifaceId, ifaceDao); TransactionProxyFactoryBean bean = new TransactionProxyFactoryBean(); bean.setTransactionManager(transMgr); bean.setTarget(agent); Properties props = new Properties(); props.put("*", "PROPAGATION_REQUIRED"); bean.setTransactionAttributes(props); bean.afterPropertiesSet(); return (CollectionAgentService) bean.getObject(); }
/** * Wraps the target (probably a BiConsumer(T, context)) with a proxy that enforces a new * spring transaction. * * Note: this might be better placed in the engine-spring directly, so it will probably be * replaced with some central implemenation. * * @param target the wrapped instance * @param <T> wrapped type * @return proxy */ protected <T> T requireNewTransaction(T target) { final TransactionProxyFactoryBean proxy = new TransactionProxyFactoryBean(); // Inject transaction manager here proxy.setTransactionManager(transactionManager); // Define wich object instance is to be proxied (your bean) proxy.setTarget(target); // Programmatically setup transaction attributes proxy.setTransactionAttributes(TRANSACTION_ATTRIBUTES); proxy.setProxyTargetClass(true); // Finish FactoryBean setup proxy.afterPropertiesSet(); return (T) proxy.getObject(); }