Two Laptop Bag*
The moose likes Other Application Frameworks and the fly likes Spring 2.5 - Transaction example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring 2.5 - Transaction example" Watch "Spring 2.5 - Transaction example" New topic
Author

Spring 2.5 - Transaction example

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
New to Spring. Using Spring 2.5 to create example of using Transaction. Not using any database connection. But trying to see how to exercise Transaction. Any help will be appreciated.

Getting exception as: Cannot resolve reference to bean 'transactionManager'

Following is Driver class
package org.poc.tx;
/**
* Similar to "Spring Recipes" from Gary Mak. Section 8.5
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Driver {
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("transaction_bookshop.xml");
BookShop bookShop = (BookShop) ctx.getBean("bookShop");
}
}

Following is the class on which transaction will be invoked:
package org.poc.tx;

public class BookShop {

public void purchase(){
System.out.println("Inside purchase");
}
}
Folliwng is the xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="bookShop" class="org.poc.tx.BookShop" />

<aop:config>
<aop ointcut
id="bookShopOperation"
expression="execution(* org.poc.tx.BookShop.*(..))"
/>
<aop:advisor
pointcut-ref="bookShopOperation"
advice-ref="bookShopTxAdvice"
/>
</aop:config>

<tx:advice id="bookShopTxAdvice">
<tx:attributes>
<tx:method
name="purchase"
read-only="true"
propagation="REQUIRED"
isolation="READ_COMMITTED"
timeout="5"
/>
</tx:attributes>
</tx:advice>
</beans>

Following is the exception that I am getting, when I run the Driver.java:


11:47:04,971 INFO ClassPathXmlApplicationContext:412 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6cac6cac: display name [org.springframework.context.support.ClassPathXmlApplicationContext@6cac6cac]; startup date [Mon Jul 28 11:47:04 EDT 2008]; root of context hierarchy
11:47:05,095 INFO XmlBeanDefinitionReader:323 - Loading XML bean definitions from class path resource [transaction_bookshop.xml]
11:47:05,173 DEBUG DefaultDocumentLoader:72 - Using JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl]
11:47:05,267 DEBUG PluggableSchemaResolver:125 - Loading schema mappings from [META-INF/spring.schemas]
11:47:05,282 DEBUG PluggableSchemaResolver:131 - Loaded schema mappings: {http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd}
11:47:05,282 DEBUG PluggableSchemaResolver:114 - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-2.5.xsd
11:47:05,391 DEBUG PluggableSchemaResolver:114 - Found XML schema [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd] in classpath: org/springframework/aop/config/spring-aop-2.5.xsd
11:47:05,407 DEBUG PluggableSchemaResolver:114 - Found XML schema [http://www.springframework.org/schema/tx/spring-tx-2.5.xsd] in classpath: org/springframework/transaction/config/spring-tx-2.5.xsd
11:47:05,423 DEBUG DefaultBeanDefinitionDocumentReader:86 - Loading bean definitions
11:47:05,454 DEBUG DefaultNamespaceHandlerResolver:153 - Loaded mappings [{http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler, http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler, http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler, http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler, http://www.springframework.org/schema/jms=org.springframework.jms.config.JmsNamespaceHandler, http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler, http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler, http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler}]
11:47:05,516 DEBUG XmlBeanDefinitionReader:185 - Loaded 5 bean definitions from location pattern [transaction_bookshop.xml]
11:47:05,516 INFO ClassPathXmlApplicationContext:427 - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@6cac6cac]: org.springframework.beans.factory.support.DefaultListableBeanFactory@605a605a
11:47:05,516 DEBUG ClassPathXmlApplicationContext:431 - 5 beans defined in org.springframework.context.support.ClassPathXmlApplicationContext@6cac6cac: display name [org.springframework.context.support.ClassPathXmlApplicationContext@6cac6cac]; startup date [Mon Jul 28 11:47:04 EDT 2008]; root of context hierarchy
11:47:05,532 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
11:47:05,532 DEBUG DefaultListableBeanFactory:383 - Creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator'
11:47:05,563 DEBUG DefaultListableBeanFactory:459 - Eagerly caching bean 'org.springframework.aop.config.internalAutoProxyCreator' to allow for resolving potential circular references
11:47:05,578 DEBUG DefaultListableBeanFactory:411 - Finished creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator'
11:47:05,578 DEBUG ClassPathXmlApplicationContext:649 - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@6f1c6f1c]
11:47:05,578 DEBUG ClassPathXmlApplicationContext:673 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@2e782e78]
11:47:05,578 INFO DefaultListableBeanFactory:414 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@605a605a: defining beans [bookShop,org.springframework.aop.config.internalAutoProxyCreator,bookShopOperation,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,bookShopTxAdvice]; root of factory hierarchy
11:47:05,578 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'bookShop'
11:47:05,578 DEBUG DefaultListableBeanFactory:383 - Creating instance of bean 'bookShop'
11:47:05,578 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
11:47:05,578 DEBUG DefaultListableBeanFactory:383 - Creating instance of bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
11:47:05,594 DEBUG DefaultListableBeanFactory:459 - Eagerly caching bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' to allow for resolving potential circular references
11:47:05,594 DEBUG DefaultListableBeanFactory:383 - Creating instance of bean 'bookShopOperation'
11:47:05,719 DEBUG DefaultListableBeanFactory:411 - Finished creating instance of bean 'bookShopOperation'
11:47:05,719 DEBUG DefaultListableBeanFactory:411 - Finished creating instance of bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
11:47:05,719 DEBUG DefaultListableBeanFactory:459 - Eagerly caching bean 'bookShop' to allow for resolving potential circular references
11:47:05,719 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
11:47:05,719 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
11:47:05,906 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'bookShopTxAdvice'
11:47:05,906 DEBUG DefaultListableBeanFactory:383 - Creating instance of bean 'bookShopTxAdvice'
11:47:05,906 DEBUG DefaultListableBeanFactory:459 - Eagerly caching bean 'bookShopTxAdvice' to allow for resolving potential circular references
11:47:05,921 INFO DefaultListableBeanFactory:399 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@605a605a: defining beans [bookShop,org.springframework.aop.config.internalAutoProxyCreator,bookShopOperation,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,bookShopTxAdvice]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookShop' defined in class path resource [transaction_bookshop.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookShopTxAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.poc.tx.Driver.main(Driver.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookShopTxAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(AccessController.java:219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor.getAdvice(AbstractBeanFactoryPointcutAdvisor.java:77)
at org.springframework.aop.aspectj.AspectJProxyUtils.isAspectJAdvice(AspectJProxyUtils.java:68)
at org.springframework.aop.aspectj.AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(AspectJProxyUtils.java:51)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.extendAdvisors(AspectJAwareAdvisorAutoProxyCreator.java:100)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:89)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:66)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:360)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1343)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 14 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:968)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 37 more
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi,

just read your errormessage again
Error creating bean with name 'bookShopTxAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined.

You need to define a 'transactionManager'.

Herman
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring 2.5 - Transaction example
 
Similar Threads
Problem using EclipseLink -> org.springframework.dao.InvalidDataAccessApiUsageException
Spring AOP DTD ...
Spring AOP TX management error...
Spring JDBC Problem getting log messages
Spring AOP aspect not working.