javax.naming.NameNotFoundException: le nom jdbc n’est pas lié dans ce contexte

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

Problème

Application Web JSF 2.0, un bean géré utilise@Resource pour injecter la source de données «jdbc/exampledb» dans une propriété ds.

@ManagedBean(name="customer")
@SessionScoped
public class CustomerBean implements Serializable{

    //resource injection
    @Resource(name="jdbc/exampledb")
    private DataSource ds;

Lorsqu'il est déployé sur Tomcat 6, il rencontre les messages d'erreur suivants pour la configuration de la source de données MySQL.

com.sun.faces.mgbean.ManagedBeanCreationException:
        An error occurred performing resource injection on managed bean customer
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:207)
Caused by: com.sun.faces.spi.InjectionProviderException:
        javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    at com.sun.faces.vendor.Tomcat6InjectionProvider.inject(Tomcat6InjectionProvider.java:84)
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:201)
    ... 53 more
Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    ... 54 more

Solution

La source de données «jdbc / exampledb» n'est pas configurée correctement dans Tomcat, consultez ce guide pour plus de détails -How to configure MySQL DataSource in Tomcat 6