论坛首页 Java企业应用论坛

jboss下配置jndi利用spring的IOC来获得

浏览 4724 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-06  

本来项目内用的是普通数据源.考虑没有分布式的数据访问.就使用了apache-commons-dbcp.但是后来项目内要加一个第三方的报表制作工具.该工具强制要求使用JNDI来获得连接(该死的)没办法.只有加了.显将配置步骤和一个错误做笔记如下:

1. 环境: windowsXp系统.使用的是jbossGA-4.2.2 sqlserver2000.
项目架构是 Spring+ibatis+struts.
2. 配置步骤1 . 由于jboss会自动查找server\default\deploy目录下的 **-ds.xml文件.并读取其中内容.来得到相应的DataSource,我的文件具体内容如下:
<?xml version="1.0" encoding="UTF-8"?>
     <datasources> 
     <local-tx-datasource>
           <jndi-name>jdbc/DataSource</jndi-name>
           <connection-url>jdbc:jtds:sqlserver://localhost:1433/fnx</connection-url>
           <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
           <user-name>sa</user-name>
           <password>1234</password>

          <metadata> 
          <type-mapping>MS SQLSERVER2000</type-mapping>
          </metadata>
          </local-tx-datasource>
</datasources>

配置步骤2 . (有部分内容来自网上) 由于使用spring来获取jndi.所以实现了FactoryBean, InitializingBean

get/set方法没加

 
public class DataSourceFactoryBean implements FactoryBean, InitializingBean {

 /** 缓存数据源对象 */
 private DataSource dataSource;
 /** 是否jndi数据源 */
 private boolean jndi;
 /** 数据源的名字(如果是jndi数据源) */
 private String jndiName;
 /** jdbc驱动类(非jndi的情况) */
 private String driverClassName;
 /** 数据库的url(非jndi的情况) */
 private String url;
 /** 用户名(非jndi的情况) */
 private String username;
 /** 密码(非jndi的情况) */
 private String password;

 public void afterPropertiesSet() throws Exception {
  if (this.isJndi()) {
   if (!org.springframework.util.StringUtils.hasText(this.jndiName)) {
    throw new IllegalArgumentException("jndiName is required");
   }
  } else {
   if (!org.springframework.util.StringUtils
     .hasText(this.driverClassName)) {
    throw new IllegalArgumentException(
      "driverClassName is required");
   }
   if (!org.springframework.util.StringUtils.hasText(this.url)) {
    throw new IllegalArgumentException("url is required");
   }
   if (!org.springframework.util.StringUtils.hasText(this.username)) {
    throw new IllegalArgumentException("username is required");
   }
   if (!org.springframework.util.StringUtils.hasText(this.password)) {
    throw new IllegalArgumentException("password is required");
   }
  }
  // 在初始化时就创建数据源
  this.createDataSource();
 }

 public Object getObject() throws Exception {
  DataSource ds = this.createDataSource();
  return ds;
 }

 public Class getObjectType() {
  return javax.sql.DataSource.class;
 }

 public boolean isSingleton() {
  return true;
 }

 protected DataSource createDataSource() {
  DataSource ds = this.dataSource;
  if (ds == null) {
   try {
    if (this.isJndi()) {
     ds = new JndiDataSourceSupport()
       .lookupDataSource(this.jndiName);
    } else {
     ds = new DriverManagerDataSource(driverClassName, url,
       username, password);
    }
   } catch (Throwable err) {
    throw new RuntimeException(err);
   }
  }
  this.dataSource = ds;
  return ds;
 }

 private class JndiDataSourceSupport extends JndiLocatorSupport {
  public DataSource lookupDataSource(String jndiName)
    throws NamingException {
   return (DataSource) super.lookup(jndiName,getObjectType());
  }
 }

}

 

配置步骤3.  spring的配置文件片段

 <bean id="dataSource" class="****&*#*****.dao.DataSourceFactoryBean">
     <property name="jndi" value="${jdbc.IS_JNDI}" />
     <property name="jndiName" value="${jdbc.jndiName}" />
     <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>
 
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:config/jdbc.properties</value>
   </list>
  </property>
 </bean>

 

设置步骤4 . jdbc.properties  

 

#false 为开发时候使用的传统datasource 。为true为使用jndi得到datasource。建议开发时候使用前者
jdbc.IS_JNDI=true
jdbc.jndiName=java:jdbc/DataSource

#sql
jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.url=jdbc:jtds:sqlserver://localhost:1433/fnx
jdbc.username=sa
jdbc.password=1234

 

 

设置步骤5  一些说明.上面绿色部分要相同.jndi的datasource采用的而是jdts.开始的时候总是有错.第一次无法的到连接.

 刷新后就可以得到连接.百思不得其解.经过查找发现自己大意了.没有吧jdts的jar放在jboss4.2.2GA\server\default\lib下面.解决和异常如下:

jboss jndi jtds sqlserver2000 第一次错误得不到连接.然后刷新页面就ok的问题.
    问题搞定.
   就是jar没加对.
   jdni属于容器的应该加在容器内.我们加载在项目内.当然第一次会出错.
   然后第二次项目内的jar就会加载到容器你.所以就可用了.
   应该在jboss4.2.2GA\server\default\lib 加数据库jar就可以了.

  错误异常如下:
   Throwable while attempting to get a new connection: null
org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Apparently wrong driver class specified for URL: class: net.sourceforge.jtds.jdbc.Driver, url: jdbc:jtds:sqlserver://localhost:1433/fnx)
 at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:179)
 at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.createConnectionEventListener(InternalManagedConnectionPool.java:577)
 at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.getConnection(InternalManagedConnectionPool.java:262)
 at org.jboss.resource.connectionmanager.JBossManagedConnectionPool$BasePool.getConnection(JBossManagedConnectionPool.java:500)
 at org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:341)
 at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:315)
 at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:396)
 at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:842)
 at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:88)
 at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:182)
 at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:322)
 at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:255)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
 at $Proxy56.getUserByPassword(Unknown Source)
 at com.thams.user.action.LoginAction.gotoLogin(LoginAction.java:43)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:585)
 at com.thams.framework.action.BaseAction.dispatchMethod(BaseAction.java:205)
 at com.thams.framework.action.BaseAction.execute(BaseAction.java:129)
 at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
 at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at com.thams.framework.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:58)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:595)
Caused by: org.jboss.resource.JBossResourceException: Apparently wrong driver class specified for URL: class: net.sourceforge.jtds.jdbc.Driver, url: jdbc:jtds:sqlserver://localhost:1433/fnx
 at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.getDriver(LocalManagedConnectionFactory.java:290)
 at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:170)
 ... 50 more

 

   发表时间:2008-08-06  
jndiObjectFactoryBean
方便灵活切换.开发时候使用jndi无法脱离容器调试.所以才写了那个类.哈哈哈.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics