提问者:小点点

SpringMVCJNDI连接池错误


我正在使用SpringMVCJNDI,我得到了这个异常:

异常javax. nameNotFoundException:名称jdbc在此上下文中未绑定

我不知道如何解决这个问题。这是我的代码:

应用上下文. xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dbDataSource" ref="dbDataSource" />  
</beans>

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/practice"/>
  <property name="lookupOnStartup" value="true"></property>
</bean>

public class EmpDAOImpl implements EmpDAO {
  @Autowired
  private SessionFactory sessionfactory;

  @Autowired
  @Qualifier("dbDataSource")
  private DataSource dataSource;
}

web. xml

<web-app >
  <resource-ref>
    <description>MySQL Datasource example</description>
    <res-ref-name>java:comp/env/jdbc/practice</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

共3个答案

匿名用户

您不是在说如何在容器中配置JNDI资源(例如Tomcat中的server. xmlMETA-INF/context.xml或…)。但是中的资源名称

这里有一些建议:

  • http://www.gridshore.nl/2007/11/16/loading-properties-using-jndi-within-the-springframework-deployed-on-tomcat/
  • http://www.mkyong.com/tomcat/how-to-configure-mysql-datasource-in-tomcat-6/
  • http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

匿名用户

 applicationContext.xml
 <bean id="sessionFactory"                                                                                                     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dbDataSource" ref="dbDataSource" />  
  </bean>
 <bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/practice"/>
     <property name="lookupOnStartup" value="true"></property>
  </bean>

 web.xml
  <resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>java:comp/env/jdbc/practice</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
 </resource-ref>

公共类MPDAOImpl实现了MPDAO{

@Autowired
private SessionFactory sessionfactory;

    @Autowired
     @Qualifier("dbDataSource")
    private DataSource dataSource;

公开无效addEmp(员工){

    this.sessionfactory.getCurrentSession().save(e);

     }
      }

   server.xml
  <server>
  <GlobalNamingResources>
  <Resource name="jdbc/practice"
  global="jdbc/practice"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/practice"
  username="root"
  password="root123"
  maxActive="100"
  maxIdle="20"
  minIdle="5"
  maxWait="10000"/>
  </GlobalNamingResources>
    </server>

匿名用户

context. xml

   <context>
    <ResourceLink name="jdbc/practice"
                global="jdbc/practice"
                auth="Container"
                type="javax.sql.DataSource" />
    </context>