Spring自定义DataSource与WebSphere服务器上的JNDI连接 [英] Spring Custom DataSource Connection with JNDI on WebSphere server

查看:161
本文介绍了Spring自定义DataSource与WebSphere服务器上的JNDI连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与post相同的要求,以传递用户标识作为用于审计目的的客户端标识符。
在syscontext上传递ClientInfo / ClientIdentifier /为了审计目的而使用Hibernate连接



我遵循了与 8.2中提到的相同方法。自定义数据源连接准备程序的配置 http://docs.spring.io/spring-data/jdbc/docs/current/reference/html/orcl.connection .html



下面是我有的一段代码:

  @Aspect 
public class ClientIdentifierConnectionPreparer {

@AfterReturning(pointcut =execution(* javax.sql.DataSource.getConnection(..)),
回覆转换=连接)
公共连接setClientIdentifier(连接连接)抛出SQLException {
CallableStatement cs = connection.prepareCall({call DBMS_SESSION.SET_IDENTIFIER('XXXX')});
cs.execute();
cs.close();
返回连接;
}

}



 < aop:aspectj-autoproxy />我已经配置 aop autoaspect proxy  ; 

class =xx.xxxx.xxxx.xxxx.xxxx.ClientIdentifierConnectionPreparer/>

我正在 websphere server 中部署我的网络服务。一切工作正常如果我使用 apache dbcp BasicDataSource 配置数据源:

 < bean id = dataSourceclass =org.apache.commons.dbcp.BasicDataSource> 
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>

使用上述配置,代码正常工作。一旦连接返回,我能够设置值为客户端标识符,并能够通过审计触发器从syscontext中检索它。



但是我们有如下所示的实际数据源配置:

 < bean id =dataSourceclass =org.springframework.jndi.JndiObjectFactoryBean> 
< property name =jndiNamevalue =jdbc / myDS>< / property>



我无法将此配置更改为它被多个应用程序使用,我必须遵循相同的步骤。通过上面的配置在部署时,我在创建数据源bean时收到空指针异常。下面是我得到的异常:

 原因:org.springframework.beans.factory.BeanCreationException:创建bean with名称'sessionFactory'在类路径resource [beans.xml]中定义:调用init方法失败;嵌套异常是org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)中的java.lang.NullPointerException 
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中的
。 doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1 .getObject(AbstractBeanFactory.java:304)
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
在org.springframework.beans.factory.support.AbstractBeanFactory .doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 121 more
引起:java.lang.NullPointerException
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection( WSJdbcDataSource.java:511)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl $ ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
位于org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
位于org.hibernate.service.internal.StandardServiceRegistryImpl。 (ServiceServiceRegistryImpl.java:75)
在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl。 java:131)
在org.hibernate。 cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
位于org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
位于org.hibernate.cfg.Configuration.buildSessionFactory(配置。 Java的:1861)在org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343

在org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)$ b处$ b at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)



<有人曾经采用过这种方法。如果有人能够提供关于如何进一步解决这个错误的指示,那将会很有帮助。
是否有其他建议的方法可以采用这种方法。提前致谢!



UPDATE ::



我尝试使用ProxyInterface按照下面的答案指定类型。

  DEBUG  - 创建bean的实例'sessionFactory' 
DEBUG - 快速缓存bean'sessionFactory'以允许解决潜在的循环引用
DEBUG - 返回单例bean'dataSource'的缓存实例
DEBUG - 返回单例bean的缓存实例org.springframework .transaction.config.internalTransactionAdvisor'
DEBUG - 返回单例bean的缓存实例'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
DEBUG - 返回单例bean的缓存实例'org.springframework.transaction .config.internalTransactionAdvisor'
DEBUG - 返回singleton bean的缓存实例'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
DEBUG - 为bean'dataSource'创建带有0个通用拦截器和2个隐式代理特定的拦截器
DEBUG - 创建CGLIB代理:目标源是目标对象的SingletonTargetSource [com.sun.proxy.$Proxy134@df1d109a]
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.equals(java.lang。对象)],因为它是最终的:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final int com.sun.proxy。$ Proxy134.hashCode()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final java.lang.String com.sun.proxy。$ Proxy134.toString()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isPreFiltered()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final java.io.PrintWriter com.sun.proxy。$ Proxy134.getLogWriter()throws java.sql.SQLException]因为它是final:通过代理对此方法的所有调用不会被路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isProxyTargetClass()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isFrozen()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.setLogWriter(java.io.PrintWriter)throws java.sql.SQLException]因为它是final:所有对此方法的调用都是通过代理将不会路由到目标实例。
WARN - 无法代理方法[public final java.lang.Object com.sun.proxy。$ Proxy134.unwrap(java.lang.Class)throws java.sql.SQLException]因为它是final的:所有对这个方法通过代理不会被路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.removeAdvice(org.aopalliance.aop.Advice)],因为它是final的:所有通过代理的方法调用都不是路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.removeAdvisor(org.springframework.aop.Advisor)],因为它是最终的:所有通过代理的方法调用都不是路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.setExposeProxy(boolean)]因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final int com.sun.proxy。$ Proxy134.indexOf(org.springframework.aop.Advisor)],因为它是最终的:所有通过代理对此方法的调用都不会路由到目标实例。
WARN - 无法代理方法[public final java.lang.Class com.sun.proxy。$ Proxy134.getTargetClass()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final org.springframework.aop.TargetSource com.sun.proxy。$ Proxy134.getTargetSource()],因为它是最终的:所有通过代理对此方法的调用都不会被路由到目标实例。
WARN - 无法代理方法[public final int com.sun.proxy。$ Proxy134.getLoginTimeout()throws java.sql.SQLException]因为它是final:通过代理对此方法的所有调用都不会被路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.addAdvisor(int,org.springframework.aop.Advisor)throws org.springframework.aop.framework.AopConfigException]因为它是final :通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isExposeProxy()],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.addAdvisor(org.springframework.aop.Advisor)throws org.springframework.aop.framework.AopConfigException]因为它是final:全部通过代理调用此方法将不会路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isWrapperFor(java.lang.Class)throws java.sql.SQLException]因为它是final:所有对此方法的调用都通过代理将不会路由到目标实例。
WARN - 无法代理方法[public final java.sql.Connection com.sun.proxy。$ Proxy134.getConnection()throws java.sql.SQLException]因为它是final:通过代理对此方法的所有调用不会被路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.addAdvice(int,org.aopalliance.aop.Advice)throws org.springframework.aop.framework.AopConfigException]因为它是final :通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.setLoginTimeout(int)throws java.sql.SQLException]因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final java.lang.Class [] com.sun.proxy。$ Proxy134.getProxiedInterfaces()],因为它是final:通过代理对此方法的所有调用都不会被路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.removeAdvisor(int)throws org.springframework.aop.framework.AopConfigException]因为它是final的:所有对此方法的调用都通过代理将不会路由到目标实例。
WARN - 无法代理方法[public final java.sql.Connection com.sun.proxy。$ Proxy134.getConnection(java.lang.String,java.lang.String)throws java.sql.SQLException]因为它是最终的:通过代理对这个方法的所有调用都不会被路由到目标实例。
WARN - 无法代理方法[public final int com.sun.proxy。$ Proxy134.indexOf(org.aopalliance.aop.Advice)],因为它是最终的:通过代理对此方法的所有调用将不会路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.setPreFiltered(boolean)]因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final boolean com.sun.proxy。$ Proxy134.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor)throws org.springframework.aop.framework。 AopConfigException],因为它是最终的:通过代理对此方法的所有调用都不会路由到目标实例。
无法代理方法[public final org.springframework.aop.Advisor [] com.sun.proxy。$ Proxy134.getAdvisors()],因为它是最终的:通过代理对此方法的所有调用都不会被路由到目标实例。
无法代理方法[public final boolean com.sun.proxy。$ Proxy134.isInfacefaceProxied(java.lang.Class)],因为它是final:通过代理对此方法的所有调用都不会路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.addAdvice(org.aopalliance.aop.Advice)throws org.springframework.aop.framework.AopConfigException]因为它是final:全部通过代理调用此方法将不会路由到目标实例。
WARN - 无法代理方法[public final java.util.logging.Logger com.sun.proxy。$ Proxy134.getParentLogger()throws java.sql.SQLFeatureNotSupportedException]因为它是final的:所有对此方法的调用都是通过代理将不会路由到目标实例。
WARN - 无法代理方法[public final java.lang.String com.sun.proxy。$ Proxy134.toProxyConfigString()],因为它是final:通过代理对此方法的所有调用都不会被路由到目标实例。
WARN - 无法代理方法[public final void com.sun.proxy。$ Proxy134.setTargetSource(org.springframework.aop.TargetSource)]因为它是final:通过代理对此方法的所有调用将不会路由到目标实例。
DEBUG - 取出的依赖bean for bean'(内部bean)#ca01323a':[org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]
破坏org.springframework.beans.factory.support中的单例。 DefaultListableBeanFactory @ 184af104:
定义bean [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context .annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.internalPersistenceAnnotationProcessor,transitionController,dataSource,$ b $ sessionFactory,transactionManager,persistenceExceptionTranslationPostProcessor,
org.springframework.aop.config.internalAutoProxyCreator,org.springframework .transaction.annotation.AnnotationTransactionAttributeSource#0,
org.springframework.transaction.interceptor.TransactionInterceptor#0,org .springframework.transaction.config.internalTransactionAdvisor
org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,
切入点,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation。 ConfigurationClassPostProcessor.enhancedConfigurationProcessor];
工厂层次结构的根
bean'(内部bean)##c0432d95'的取回依赖bean:[(内部bean)#ca01323a]
错误 - 上下文初始化失败
导致: org.springframework.beans.factory.BeanCreationException:在类路径资源[sessionfactory.xml]中定义名称为'sessionFactory'的bean时出错:在设置bean属性'dataSource'时无法解析对bean'dataSource'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'dataSource'的bean时出错:FactoryBean对象的后处理失败;嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class com.sun.proxy。$ Proxy134]的CGLIB子类:此问题的常见原因包括使用最终类或不可见类;嵌套异常是java.lang.IllegalArgumentException:无法继承最终类类com.sun.proxy。$ Proxy134
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
。在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
a吨org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304)
。在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 129 more
导致:org.springframework.beans.factory.BeanCreationException:创建错误名为'dataSource'的bean:FactoryBean对象的后处理失败;嵌套异常是org.springframework.aop.framework.AopConfigException:无法生成类[class com.sun.proxy。$ Proxy134]的CGLIB子类:此问题的常见原因包括使用最终类或不可见类;嵌套异常是java.lang.IllegalArgumentException:无法继承最终类类com.sun.proxy。$ Proxy134
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:167)
。在org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
在org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514)
。在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 139 more
引起:org.springframework.aop.framework.AopConfigException:Could not根erate类[com.sun.proxy。$ Proxy134]的CGLIB子类:此问题的常见原因包括使用最终类或不可见类;嵌套异常是java.lang.IllegalArgumentException:无法继承最终类类com.sun.proxy。$ Proxy134
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:212)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
在org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:494)
。在组织。 springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:379)
在org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:339)
。在组织。 springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryB ean(AbstractAutowireCapableBeanFactory.java:1698)
在org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:164)
... 144 more
由java导致:java .lang.IllegalArgumentException:无法继承最终类类com.sun.proxy。$ Proxy134
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
at org.springframework。 cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
在org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
在org.springframework.cglib.core。 AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at org.springframework.cglib.proxy.Enhancer.createClass( Enhancer.java:317)
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createP roxyClassAndInstance(ObjenesisCglibAopProxy.java:57)
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:202)
... 151 more


解决方案

根据WebSphere文档,数据源使用 org.springframework.jndi.JndiObjectFactoryBean 应该定义一个 proxyInterface javax.sql.DataSource



例如:

 < bean id =dataSourceclass =org.springframework .jndi.JndiObjectFactoryBean> 
< property name =jndiNamevalue =jdbc / myDS/>
< property name =lookupOnStartupvalue =false/>
< property name =cachevalue =true/>
< property name =proxyInterfacevalue =javax.sql.DataSource/>
< / bean>

来源:配置对Spring应用程序数据源的访问权限


I have a same requirement as specified in post to pass user id as client identifier for audit purposes. Passing ClientInfo/ClientIdentifier on syscontext/connection with Hibernate for audit purposes

I have followed the same approach as mentioned in 8.2.Configuration of a Custom DataSource Connection Preparer http://docs.spring.io/spring-data/jdbc/docs/current/reference/html/orcl.connection.html

Below is the piece of code which I have :

@Aspect
public class ClientIdentifierConnectionPreparer {

@AfterReturning(pointcut= "execution(* javax.sql.DataSource.getConnection(..))",
        returning = "connection")
public Connection setClientIdentifier(Connection connection) throws SQLException {
    CallableStatement cs=connection.prepareCall("{call DBMS_SESSION.SET_IDENTIFIER('XXXX')}");
    cs.execute();
    cs.close();
    return connection;
}

}

I have configured aop autoaspect proxy as shown below.

<aop:aspectj-autoproxy />

    <bean id="connectionPreparer" 
        class="xx.xxxx.xxxx.xxxx.xxxx.ClientIdentifierConnectionPreparer" />

I am deploying my web service in websphere server. Everything is working fine If I configure data source using apache dbcp BasicDataSource:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

Code is working fine with the above configuration. Once connection is returned, I am able to set value to client identifier and able to retrieve it from syscontext with audit trigger.

But We have actual data source configuration as shown below:

 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="jdbc/myDS"></property>

I cannot change this configuration as it is used by multiple applications and I have to follow the same. With the above configuration While deploying I am getting null pointer exception while creating data source bean. Below is the exception that I am getting:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 121 more
Caused by: java.lang.NullPointerException
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:511)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)    

has anyone followed this approach before. It would be helpful if some one can provide instructions on how to proceed further to to resolve this error. is there any other recommended way to follow this approach. Thanks in advance!

UPDATE::

I have tried with using ProxyInterface type as specified in below answer. I am able to resolve null pointer exception but I am getting errors with cglib proxies.

    DEBUG - Creating instance of bean 'sessionFactory'
DEBUG - Eagerly caching bean 'sessionFactory' to allow for resolving potential circular references
DEBUG - Returning cached instance of singleton bean 'dataSource'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
DEBUG - Creating implicit proxy for bean 'dataSource' with 0 common interceptors and 2 specific interceptors
DEBUG - Creating CGLIB proxy: target source is SingletonTargetSource for target object [com.sun.proxy.$Proxy134@df1d109a]
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.equals(java.lang.Object)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final int com.sun.proxy.$Proxy134.hashCode()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.lang.String com.sun.proxy.$Proxy134.toString()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isPreFiltered()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.io.PrintWriter com.sun.proxy.$Proxy134.getLogWriter() throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isProxyTargetClass()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isFrozen()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.setLogWriter(java.io.PrintWriter) throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.lang.Object com.sun.proxy.$Proxy134.unwrap(java.lang.Class) throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.removeAdvice(org.aopalliance.aop.Advice)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.removeAdvisor(org.springframework.aop.Advisor)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.setExposeProxy(boolean)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final int com.sun.proxy.$Proxy134.indexOf(org.springframework.aop.Advisor)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.lang.Class com.sun.proxy.$Proxy134.getTargetClass()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final org.springframework.aop.TargetSource com.sun.proxy.$Proxy134.getTargetSource()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final int com.sun.proxy.$Proxy134.getLoginTimeout() throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.addAdvisor(int,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isExposeProxy()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.addAdvisor(org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isWrapperFor(java.lang.Class) throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.sql.Connection com.sun.proxy.$Proxy134.getConnection() throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.addAdvice(int,org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.setLoginTimeout(int) throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.lang.Class[] com.sun.proxy.$Proxy134.getProxiedInterfaces()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.removeAdvisor(int) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.sql.Connection com.sun.proxy.$Proxy134.getConnection(java.lang.String,java.lang.String) throws java.sql.SQLException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final int com.sun.proxy.$Proxy134.indexOf(org.aopalliance.aop.Advice)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.setPreFiltered(boolean)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
Unable to proxy method [public final org.springframework.aop.Advisor[] com.sun.proxy.$Proxy134.getAdvisors()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
Unable to proxy method [public final boolean com.sun.proxy.$Proxy134.isInterfaceProxied(java.lang.Class)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.addAdvice(org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.util.logging.Logger com.sun.proxy.$Proxy134.getParentLogger() throws java.sql.SQLFeatureNotSupportedException] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final java.lang.String com.sun.proxy.$Proxy134.toProxyConfigString()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
WARN  - Unable to proxy method [public final void com.sun.proxy.$Proxy134.setTargetSource(org.springframework.aop.TargetSource)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.
DEBUG - Retrieved dependent beans for bean '(inner bean)#ca01323a': [org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]
Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@184af104: 
defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.internalPersistenceAnnotationProcessor,transitionController,dataSource,
sessionFactory,transactionManager,persistenceExceptionTranslationPostProcessor,
org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,
org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor
org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,
pointCut,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; 
root of factory hierarchy
Retrieved dependent beans for bean '(inner bean)#c0432d95': [(inner bean)#ca01323a]
 ERROR - Context initialization failed
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [sessionfactory.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Post-processing of the FactoryBean's object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy134]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy134
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    ... 129 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Post-processing of the FactoryBean's object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy134]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy134
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:167)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    ... 139 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy134]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy134
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:212)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:494)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:379)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:339)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1698)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:164)
    ... 144 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy134
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:202)
    ... 151 more

解决方案

Per WebSphere documentation, data sources using the org.springframework.jndi.JndiObjectFactoryBean should define a proxyInterface of javax.sql.DataSource.

For example:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myDS"/>
    <property name="lookupOnStartup" value="false"/>
    <property name="cache" value="true"/>
    <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

Source: Configuring access to a Spring application data source

这篇关于Spring自定义DataSource与WebSphere服务器上的JNDI连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆