Spring MVC 3 및 MySQL 서버를 사용하고 있습니다. JDBC 연결에 JNDI를 사용하려하지만 NULL DataSource를 반환합니다. 다음은 널 포인터 예외를 던지는 코드 조각입니다.spring mvc3 통합을 사용하는 JNDI
server.xml
파일을 포함한다 :
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
context.xml
파일 내용
<Resource name="jdbc/Test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
web.xml
파일 cantain을 :
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/Test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
despatcher-servlet.xml
파일 :
package com.biztree.springtest.database;
import javax.sql.DataSource;
public class DataBaseConfiguration {
DataSource dataSource;
public DataBaseConfiguration() {
// TODO Auto-generated constructor stub
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource() {
return dataSource;
}
}
듣고
<bean name="myDataSourceInJndi" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/Test</value>
</property>
</bean>
<bean name="dbConfiguration" class="com.biztree.springtest.database.DataBaseConfiguration" >
<property name="dataSource" ref="myDataSourceInJndi" />
</bean>
0
/* this code work throw NullPointerException */
try {
DataBaseConfiguration baseConfiguration = new DataBaseConfiguration();
DataSource ds = baseConfiguration.getDataSource();
System.out.println("ds Object : " + ds);
connection = ds.getConnection();
} catch (Exception exception) {
exception.printStackTrace();
}
를 연결하는 코드이지만 ds
은 null입니다. 내가 제대로 작동 그것보다 다음 코드를 사용하는 경우
/* this code work fine */
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/Test");
System.out.println("ds Object : " + ds);
connection = ds.getConnection();
} catch (Exception exception) {
exception.printStackTrace();
}
데이터 소스에 대한 ''요소는 어디에 있습니까? http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html –
axtavt
죄송합니다. axtavt, 질문에 요소를 쓰는 것을 잊었습니다. context.xml의 내 리소스 –