Announcement Announcement Module
Collapse
No announcement yet.
Proxy DataSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Proxy DataSource

    From some other posts on this forum it appears that it should be possible to proxy a DataSource. I have attempted to do this but get a strange error from Spring. Here is my setup:

    Code:
        <bean id="account" class="com.pdsisoft.AccountImpl">
            <property name="dataSource">
                <ref local="dataProxy"/>
            </property>
        </bean>
    
        <bean id="dataProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
                <ref local="myDataSource"/>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>advisor</value>
                </list>
            </property>
        </bean>
    
        <bean id="advice" class="com.pdsisoft.core.spring.DataSourceInterceptor"/>
        <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
            <property name="advice">
                <ref local="advice"/>
            </property>
            <property name="pointcut">
                <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
                    <property name="pattern">
                        <value>.*getConnection.*</value>
                    </property>
                </bean>
            </property>
        </bean>
        
        <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName">
                <value>com.informix.jdbc.IfxDriver</value>
    	</property>
    	<property name="url">
                <value>&#91;ommitted&#93;</value>
    	</property>
    	<property name="username">
                <value>&#91;ommitted&#93;</value>
    	</property>
    	<property name="password">
                <value>&#91;ommitted&#93;</value>
    	</property>
        </bean>
    When Spring starts up it immediately throws the following exception:

    Code:
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name '/getAccounts' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Can't resolve reference to bean 'account' while setting property 'account'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'account' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Can't resolve reference to bean 'dataSourceProxy' while setting property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'dataSourceProxy' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException&#58; null
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'account' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Can't resolve reference to bean 'dataSourceProxy' while setting property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'dataSourceProxy' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException&#58; null
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'dataSourceProxy' defined in ServletContext resource &#91;/WEB-INF/applicationContext.xml&#93;&#58; Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException&#58; null
    net.sf.cglib.core.CodeGenerationException&#58; java.lang.reflect.InvocationTargetException-->null
    	at net.sf.cglib.core.AbstractClassGenerator.create&#40;AbstractClassGenerator.java&#58;236&#41;
    	at net.sf.cglib.proxy.Enhancer.createHelper&#40;Enhancer.java&#58;368&#41;
    	at net.sf.cglib.proxy.Enhancer.create&#40;Enhancer.java&#58;280&#41;
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy&#40;Cglib2AopProxy.java&#58;194&#41;
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy&#40;Cglib2AopProxy.java&#58;150&#41;
    	at org.springframework.aop.framework.ProxyFactoryBean.getSingletonInstance&#40;ProxyFactoryBean.java&#58;256&#41;
    	at org.springframework.aop.framework.ProxyFactoryBean.setBeanFactory&#40;ProxyFactoryBean.java&#58;220&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;344&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;257&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;147&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference&#40;AbstractAutowireCapableBeanFactory.java&#58;1021&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary&#40;AbstractAutowireCapableBeanFactory.java&#58;944&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;885&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;714&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;331&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;257&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;147&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference&#40;AbstractAutowireCapableBeanFactory.java&#58;1021&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary&#40;AbstractAutowireCapableBeanFactory.java&#58;944&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;885&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;714&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;331&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;257&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;147&#41;
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons&#40;DefaultListableBeanFactory.java&#58;277&#41;
    	at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;319&#41;
    	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh&#40;AbstractRefreshableWebApplicationContext.java&#58;131&#41;
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext&#40;ContextLoader.java&#58;230&#41;
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext&#40;ContextLoader.java&#58;156&#41;
    	at org.springframework.web.context.ContextLoaderServlet.init&#40;ContextLoaderServlet.java&#58;81&#41;
    	at javax.servlet.GenericServlet.init&#40;GenericServlet.java&#58;256&#41;
    	at org.apache.catalina.core.StandardWrapper.loadServlet&#40;StandardWrapper.java&#58;1044&#41;
    	at org.apache.catalina.core.StandardWrapper.load&#40;StandardWrapper.java&#58;887&#41;
    	at org.apache.catalina.core.StandardContext.loadOnStartup&#40;StandardContext.java&#58;3959&#41;
    	at org.apache.catalina.core.StandardContext.start&#40;StandardContext.java&#58;4284&#41;
    	at org.apache.catalina.core.ContainerBase.addChildInternal&#40;ContainerBase.java&#58;866&#41;
    	at org.apache.catalina.core.ContainerBase.addChild&#40;ContainerBase.java&#58;850&#41;
    	at org.apache.catalina.core.StandardHost.addChild&#40;StandardHost.java&#58;638&#41;
    	at org.apache.catalina.core.StandardHostDeployer.addChild&#40;StandardHostDeployer.java&#58;839&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at org.apache.commons.beanutils.MethodUtils.invokeMethod&#40;MethodUtils.java&#58;252&#41;
    	at org.apache.commons.digester.SetNextRule.end&#40;SetNextRule.java&#58;256&#41;
    	at org.apache.commons.digester.Rule.end&#40;Rule.java&#58;276&#41;
    	at org.apache.commons.digester.Digester.endElement&#40;Digester.java&#58;1058&#41;
    	at org.apache.catalina.util.CatalinaDigester.endElement&#40;CatalinaDigester.java&#58;123&#41;
    	at org.apache.xerces.parsers.AbstractSAXParser.endElement&#40;Unknown Source&#41;
    	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement&#40;Unknown Source&#41;
    	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch&#40;Unknown Source&#41;
    	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument&#40;Unknown Source&#41;
    	at org.apache.xerces.parsers.XML11Configuration.parse&#40;Unknown Source&#41;
    	at org.apache.xerces.parsers.XML11Configuration.parse&#40;Unknown Source&#41;
    	at org.apache.xerces.parsers.XMLParser.parse&#40;Unknown Source&#41;
    	at org.apache.xerces.parsers.AbstractSAXParser.parse&#40;Unknown Source&#41;
    	at org.apache.commons.digester.Digester.parse&#40;Digester.java&#58;1567&#41;
    	at org.apache.catalina.core.StandardHostDeployer.install&#40;StandardHostDeployer.java&#58;519&#41;
    	at org.apache.catalina.core.StandardHost.install&#40;StandardHost.java&#58;906&#41;
    	at org.apache.catalina.startup.HostConfig.deployDescriptors&#40;HostConfig.java&#58;527&#41;
    	at org.apache.catalina.startup.HostConfig.deployApps&#40;HostConfig.java&#58;472&#41;
    	at org.apache.catalina.startup.HostConfig.start&#40;HostConfig.java&#58;1008&#41;
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent&#40;HostConfig.java&#58;394&#41;
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent&#40;LifecycleSupport.java&#58;166&#41;
    	at org.apache.catalina.core.ContainerBase.start&#40;ContainerBase.java&#58;1134&#41;
    	at org.apache.catalina.core.StandardHost.start&#40;StandardHost.java&#58;832&#41;
    	at org.apache.catalina.core.ContainerBase.start&#40;ContainerBase.java&#58;1126&#41;
    	at org.apache.catalina.core.StandardEngine.start&#40;StandardEngine.java&#58;521&#41;
    	at org.apache.catalina.core.StandardService.start&#40;StandardService.java&#58;519&#41;
    	at org.apache.catalina.core.StandardServer.start&#40;StandardServer.java&#58;2345&#41;
    	at org.apache.catalina.startup.Catalina.start&#40;Catalina.java&#58;594&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at org.apache.catalina.startup.Bootstrap.start&#40;Bootstrap.java&#58;297&#41;
    	at org.apache.catalina.startup.Bootstrap.main&#40;Bootstrap.java&#58;398&#41;
    Caused by&#58; java.lang.reflect.InvocationTargetException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at net.sf.cglib.core.ReflectUtils.defineClass&#40;ReflectUtils.java&#58;373&#41;
    	at net.sf.cglib.core.AbstractClassGenerator.create&#40;AbstractClassGenerator.java&#58;218&#41;
    	... 79 more
    Caused by&#58; java.lang.NoClassDefFoundError&#58; org/springframework/aop/framework/Advised
    	at java.lang.ClassLoader.defineClass0&#40;Native Method&#41;
    	at java.lang.ClassLoader.defineClass&#40;ClassLoader.java&#58;537&#41;
    	... 85 more
    What is really strange is that if I change the target for the ProxyFactoryBean to be any other bean except my datasource, Spring starts up without any problem.

    Any ideas on what is going on?

    Thanks

  • #2
    Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/framework/Advised
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :537)
    ... 85 more
    It looks like you are missing the Advised interface. Do you have the whole spring.jar or spring-aop.jar inside your classpath? Make sure there are no conflicts with previous spring versions (i.e. you have only one version of spring inside the classpath).

    Comment


    • #3
      The springg.jar file is bundeled in my war application. I noticed the calss not found exception too. But what is strange is that when I proxy another object using the ProxyFactory I have defined, spring loads up, the application runs, and the proxy works correctly. So if it was a missing classpath reference, this would blow up too.

      What I am wondering is if spring is trying to proxy the datasource once it is created and not all of springs classes have been loaded. I'm thinking of moving the spring.jar file into the server/lib folder to see if it makes any difference.

      Comment


      • #4
        I put the spring.jar file into my tomcat server/lib folder and it made no difference. I also verified that the missing class is indeed in the jar file. There are no other references to any other apring classes anywhere in my classpath.

        And as I said in my original post, making the proxied class any other class except my datasource works. For example, this configuration runs wiht no errors at all:

        Code:
            <bean name="/userRegistration" class="com.pdsisoft.UserRegistrationAction" singleton="false">
                <property name="registration">
                    <ref bean="dataProxy"/>
                </property>
            </bean>
        
            <bean id="registerTrue" class="com.pdsisoft.RegistrationImplAlwaysTrue" singleton="false"/>
        
            <bean id="dataProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="target">
                    <ref local="registerTrue"/>
                </property>
                <property name="interceptorNames">
                    <list>
                        <value>advisor</value>
                    </list>
                </property>
            </bean>
        
            <bean id="advice" class="com.pdsisoft.core.spring.DataSourceInterceptor"/>
            <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
                <property name="advice">
                    <ref local="advice"/>
                </property>
                <property name="pointcut">
                    <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
                        <property name="pattern">
                            <value>.*doRegistration.*</value>
                        </property>
                    </bean>
                </property>
            </bean>
        No classpath exceptions are thrown, and the proxy works ( I have a println statement in it to let me know it was fired, and it does go off ).

        So how can proxying a datasource cause a problem, but proxying another class, with the same ProxyFactoryBean setup not work?

        I'm stumped.

        Comment


        • #5
          If you are using JBoss then most probably you have some jars (like the datasource library) placed inside server/lib and spring inside the war which does lead to CNF due to the way JBoss classloader works. There are some flags which you can set but they are not very well documented and might lead to conflicts afterwards so it's better to place all your libraries inside the same dir (i.e. server/lib).

          Comment


          • #6
            We wrote the posts at the same time - talking about concurrency .
            Take a look at the DBCP datasource class - maybe it's final, abstract or just an interface and try using a different datasource like c3p0.

            Comment


            • #7
              Costin,

              Your first post was dead on the money!

              I am using Tomcat 5 and I had suspected earlier that it had to do something with the order classes were being loaded in Tomcat. So I moved the spring jar files into the common\lib folder (the Tomcat docs say that what is in there will be loaded before any other application specific code). This ensured that Spring would be loaded first before my datasource which was proxied. Once I did this everything worked fine. I can now successfully proxy my data source.

              Thanks for the help.

              Comment


              • #8
                Allright! Anyways make sure you have a clean classpath - it will save you some trouble in the long term.

                Comment

                Working...
                X