private void writeObject(ObjectOutputStream oos) throws IOException { // Rely on default serialization, although this class itself doesn't carry state anyway... oos.defaultWriteObject(); // Deserialize superclass fields. oos.writeObject(getTransactionManagerBeanName()); oos.writeObject(getTransactionManager()); oos.writeObject(getTransactionAttributeSource()); oos.writeObject(getBeanFactory()); }
/** * Template method to create an advised object given the * target object and transaction setup. * Creates a TransactionInterceptor and applies it. */ @Override protected Object advised(Object target, PlatformTransactionManager ptm, TransactionAttributeSource tas) { TransactionInterceptor ti = new TransactionInterceptor(); ti.setTransactionManager(ptm); assertEquals(ptm, ti.getTransactionManager()); ti.setTransactionAttributeSource(tas); assertEquals(tas, ti.getTransactionAttributeSource()); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(0, ti); return pf.getProxy(); }
/** * A TransactionInterceptor should be serializable if its * PlatformTransactionManager is. */ @Test public void serializableWithAttributeProperties() throws Exception { TransactionInterceptor ti = new TransactionInterceptor(); Properties props = new Properties(); props.setProperty("methodName", "PROPAGATION_REQUIRED"); ti.setTransactionAttributes(props); PlatformTransactionManager ptm = new SerializableTransactionManager(); ti.setTransactionManager(ptm); ti = (TransactionInterceptor) SerializationTestUtils.serializeAndDeserialize(ti); // Check that logger survived deserialization assertNotNull(ti.logger); assertTrue(ti.getTransactionManager() instanceof SerializableTransactionManager); assertNotNull(ti.getTransactionAttributeSource()); }
private void writeObject(ObjectOutputStream oos) throws IOException { // Rely on default serialization, although this class itself doesn't carry state anyway... oos.defaultWriteObject(); // Deserialize superclass fields. oos.writeObject(getTransactionManagerBeanName()); oos.writeObject(getTransactionManager()); oos.writeObject(getTransactionAttributeSource()); oos.writeObject(getBeanFactory()); }
@Test public void serializableWithCompositeSource() throws Exception { NameMatchTransactionAttributeSource tas1 = new NameMatchTransactionAttributeSource(); Properties props = new Properties(); props.setProperty("methodName", "PROPAGATION_REQUIRED"); tas1.setProperties(props); NameMatchTransactionAttributeSource tas2 = new NameMatchTransactionAttributeSource(); props = new Properties(); props.setProperty("otherMethodName", "PROPAGATION_REQUIRES_NEW"); tas2.setProperties(props); TransactionInterceptor ti = new TransactionInterceptor(); ti.setTransactionAttributeSources(tas1, tas2); PlatformTransactionManager ptm = new SerializableTransactionManager(); ti.setTransactionManager(ptm); ti = (TransactionInterceptor) SerializationTestUtils.serializeAndDeserialize(ti); assertTrue(ti.getTransactionManager() instanceof SerializableTransactionManager); assertTrue(ti.getTransactionAttributeSource() instanceof CompositeTransactionAttributeSource); CompositeTransactionAttributeSource ctas = (CompositeTransactionAttributeSource) ti.getTransactionAttributeSource(); assertTrue(ctas.getTransactionAttributeSources()[0] instanceof NameMatchTransactionAttributeSource); assertTrue(ctas.getTransactionAttributeSources()[1] instanceof NameMatchTransactionAttributeSource); }
private void writeObject(ObjectOutputStream oos) throws IOException { // Rely on default serialization, although this class itself doesn't carry state anyway... oos.defaultWriteObject(); // Deserialize superclass fields. oos.writeObject(getTransactionManagerBeanName()); oos.writeObject(getTransactionManager()); oos.writeObject(getTransactionAttributeSource()); oos.writeObject(getBeanFactory()); }
PlatformTransactionManager getTransactionManagerSetOnJobRepository(JobRepository jobRepository) throws Exception { TargetSource targetSource = ((Advised) jobRepository).getTargetSource(); // proxy created in SimpleBatchConfiguration.createLazyProxy Advised target = (Advised) targetSource.getTarget(); // initial proxy created in AbstractJobRepositoryFactoryBean.initializeProxy Advisor[] advisors = target.getAdvisors(); for (Advisor advisor : advisors) { if (advisor.getAdvice() instanceof TransactionInterceptor) { TransactionInterceptor transactionInterceptor = (TransactionInterceptor) advisor.getAdvice(); return transactionInterceptor.getTransactionManager(); } } return null; }
private void writeObject(ObjectOutputStream oos) throws IOException { // Rely on default serialization, although this class itself doesn't carry state anyway... oos.defaultWriteObject(); // Deserialize superclass fields. oos.writeObject(getTransactionManagerBeanName()); oos.writeObject(getTransactionManager()); oos.writeObject(getTransactionAttributeSource()); oos.writeObject(getBeanFactory()); }
@Test public void serializable() throws Exception { TestBean1 tb = new TestBean1(); CallCountingTransactionManager ptm = new CallCountingTransactionManager(); AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource(); TransactionInterceptor ti = new TransactionInterceptor(ptm, tas); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(ITestBean1.class); proxyFactory.addAdvice(ti); proxyFactory.setTarget(tb); ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy(); proxy.getAge(); assertEquals(1, ptm.commits); ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy); serializedProxy.getAge(); Advised advised = (Advised) serializedProxy; TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice(); CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager(); assertEquals(2, serializedPtm.commits); }
private void verify(TransactionInterceptor interceptor, PlatformTransactionManager txm) throws Exception { assertSame(txm, interceptor.getTransactionManager()); TransactionAttribute atts = interceptor.getTransactionAttributeSource() .getTransactionAttribute(TransactionInterceptorBuilderTests.class.getDeclaredMethod("test"), null); Assert.assertThat(atts.getPropagationBehavior(), equalTo(Propagation.REQUIRES_NEW.value())); Assert.assertThat(atts.getIsolationLevel(), equalTo(Isolation.SERIALIZABLE.value())); Assert.assertThat(atts.getTimeout(), equalTo(42)); assertTrue(atts.isReadOnly()); }
/** * This callback is optional: If running in a BeanFactory and no transaction * manager has been set explicitly, a single matching bean of type * PlatformTransactionManager will be fetched from the BeanFactory. * @see org.springframework.beans.factory.BeanFactoryUtils#beanOfTypeIncludingAncestors * @see org.springframework.transaction.PlatformTransactionManager */ public void setBeanFactory(BeanFactory beanFactory) { if (this.transactionInterceptor.getTransactionManager() == null && beanFactory instanceof ListableBeanFactory) { ListableBeanFactory lbf = (ListableBeanFactory) beanFactory; PlatformTransactionManager ptm = (PlatformTransactionManager) BeanFactoryUtils.beanOfTypeIncludingAncestors(lbf, PlatformTransactionManager.class); this.transactionInterceptor.setTransactionManager(ptm); } }
private void verify(TransactionInterceptor interceptor, PlatformTransactionManager txm) { assertSame(txm, interceptor.getTransactionManager()); TransactionAttribute atts = interceptor.getTransactionAttributeSource() .getTransactionAttribute(null, null); assertThat(atts.getPropagationBehavior(), equalTo(Propagation.REQUIRES_NEW.value())); assertThat(atts.getIsolationLevel(), equalTo(Isolation.SERIALIZABLE.value())); assertThat(atts.getTimeout(), equalTo(42)); assertTrue(atts.isReadOnly()); }