程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> jboss下配置jndi利用spring的IOC來獲得

jboss下配置jndi利用spring的IOC來獲得

編輯:關於JAVA

來項目內用的是普通數據源.考慮沒有分布式的數據訪問.就使用了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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved