Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

This example implements the portable extension suggested in this discussion

1. Create a simple maven project with packaging jar by executing

mvn archetype:generate

and choosing option 15

2. Edit the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.seamframework.persistence</groupId>
    <artifactId>persistence.context.extension</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <finalName>persistenceContextExtension</finalName>
        <plugins>
            <!-- Compiler plugin enforces Java 1.6 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
          <groupId>javax.persistence</groupId>
          <artifactId>persistence-api</artifactId>
          <version>1.0</version>
        </dependency>
    </dependencies>
</project>

3. Add the following Java class

package org.seamframework.persistence;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Qualifier;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

/**
 * Support for managed persistence contexts in Java SE environment.
 *
 * @author Gavin King
 */
public class PersistenceContextExtension implements Extension
{

    private Bean<EntityManagerFactory> emfBean;

    /**
     * For @PersistenceContext producer fields, make a bean for the EMF, then
     * wrap the producer CDI creates, to get the EM from the EMF bean we made
     * instead of trying to get it from the Java EE component environment.
     */
    void processProducer(@Observes ProcessProducer<?, EntityManager> pp, final BeanManager bm)
    {

        if (pp.getAnnotatedMember().isAnnotationPresent(PersistenceContext.class))
        {

            if (emfBean == null)
            {
                AnnotatedField<?> field = (AnnotatedField<?>) pp.getAnnotatedMember();
                final String unitName = field.getAnnotation(PersistenceContext.class).unitName();
                final Class<?> module = field.getJavaMember().getDeclaringClass();
                final Set<Annotation> qualifiers = new HashSet<Annotation>();
                for (Annotation ann : field.getAnnotations())
                {
                    Class<? extends Annotation> annotationType = ann.annotationType();
                    //if ( bm.isQualifier(annotationType)) {
                    if (annotationType.isAnnotationPresent(Qualifier.class))
                    { //work around bug in Weld
                        qualifiers.add(ann);
                    }
                }
                if (qualifiers.isEmpty())
                {
                    qualifiers.add(new AnnotationLiteral<Default>()
                    {
                    });
                }
                qualifiers.add(new AnnotationLiteral<Any>()
                {
                });
                final boolean alternative = field.isAnnotationPresent(Alternative.class);
                final Set<Type> types = new HashSet<Type>()
                {
                    {
                        add(EntityManagerFactory.class);
                        add(Object.class);
                    } };

                //create a bean for the EMF
                emfBean = new Bean<EntityManagerFactory>()
                {
                    @Override
                    public Set<Type> getTypes()
                    {
                        return types;
                    }

                    @Override
                    public Class<? extends Annotation> getScope()
                    {
                        return ApplicationScoped.class;
                    }

                    @Override
                    public EntityManagerFactory create(CreationalContext<EntityManagerFactory> ctx)
                    {
                        return Persistence.createEntityManagerFactory(unitName);
                    }

                    @Override
                    public void destroy(EntityManagerFactory emf, CreationalContext<EntityManagerFactory> ctx)
                    {
                        emf.close();
                        ctx.release();
                    }

                    @Override
                    public Class<?> getBeanClass()
                    {
                        return module;
                    }

                    @Override
                    public Set<InjectionPoint> getInjectionPoints()
                    {
                        return Collections.EMPTY_SET;
                    }

                    @Override
                    public String getName()
                    {
                        return null;
                    }

                    @Override
                    public Set<Annotation> getQualifiers()
                    {
                        return qualifiers;
                    }

                    @Override
                    public Set<Class<? extends Annotation>> getStereotypes()
                    {
                        return Collections.EMPTY_SET; //TODO!
                    }

                    @Override
                    public boolean isAlternative()
                    {
                        return alternative;
                    }

                    @Override
                    public boolean isNullable()
                    {
                        return false;
                    }
                };

            }
            else
            {
                throw new RuntimeException("Only one EMF per application is supported");
            }

            Producer<EntityManager> producer = new Producer<EntityManager>()
            {
                @Override
                public Set<InjectionPoint> getInjectionPoints()
                {
                    return Collections.EMPTY_SET;
                }

                @Override
                public EntityManager produce(CreationalContext<EntityManager> ctx)
                {
                    return getFactory(ctx).createEntityManager();
                }

                private EntityManagerFactory getFactory(CreationalContext<EntityManager> ctx)
                {
                    return (EntityManagerFactory) bm.getReference(emfBean, EntityManagerFactory.class, ctx);
                }

                @Override
                public void dispose(EntityManager em)
                {
                    if (em.isOpen()) //work around what I suspect is a bug in Weld
                    {
                        em.close();
                    }
                }
            };
            pp.setProducer(producer);
        }
        
    }

    /**
     * Register the EMF bean with the container.
     */
    void afterBeanDiscovery(@Observes AfterBeanDiscovery abd)
    {
        abd.addBean(emfBean);
        System.out.println("finished the scanning process");

    }

    void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd)
    {

        System.out.println("beginning the scanning process");

    }


    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat)
    {

        System.out.println("scanning type: " + pat.getAnnotatedType().getJavaClass().getName());

    }


}

4. Create a file named javax.enterprise.inject.spi.Extension in the directory /src/main/resources/META-INF/services

5. Add the line org.seamframework.persistence.PersistenceContextExtension to the file created in 4.)

6. Now install the artifact by calling

mvn clean install

7. Create a webapp maven project with packaging war by executing

mvn archetype:generate

and choosing option 18 (After the project is created you have to manually add the missing directory src/main/java)

8. Edit the pom.xml of the webapp project (since I'm working behind a restrictive firewall, we have our own maven repo here, so excuse me if some dependencies don't resolve or are not necessary):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>weldTest</groupId>
    <artifactId>weldTest</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.6.ga</version>
        </dependency>
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.1.GA</version>
        </dependency>
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.1.0.GA</version>
        </dependency>
         
         <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.1_3</version>
        </dependency>
         <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.4.0.GA</version>
        </dependency>
         <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
         <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>
         
         <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
         <dependency>
            <groupId>commons-collections
            </groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging
            </groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>3.0.0.ga</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-core</artifactId>
            <version>1.0.1-CR2</version>
           
        </dependency>
        <dependency>
          <groupId>hsqldb</groupId>
          <artifactId>hsqldb</artifactId>
          <version>1.8.0.5</version>
        </dependency>
        

        <dependency>
            <groupId>org.seamframework.persistence</groupId>
            <artifactId>persistence.context.extension</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>1.0.1-CR2</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0.2-FCS</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.2-FCS</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.1.2-b04</version>
            <scope>runtime</scope>
            <exclusions>
                <exclusion>
                    <groupId>javax.el</groupId>
                    <artifactId>el-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.seamframework.persistence</groupId>
            <artifactId>persistence.context.extension</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>weldTest</finalName>
        <plugins>
            <!-- Compiler plugin enforces Java 1.6 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.0-beta-1</version>
            </plugin>
        </plugins>
    </build>

</project>

9. Add the file persistence.xml to directory /src/main/resources/META-INF and edit it as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="foo" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
                <property name="hibernate.connection.username" value="sa" />
                <property name="hibernate.connection.password" value="" />
                <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:todo" />
                <property name="hibernate.hbm2ddl.auto" value="create" />
            </properties>
        </persistence-unit>
</persistence>

10. Add the following Java class

package org.seamframework.tx;

import java.io.Serializable;

import javax.enterprise.inject.Any;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;

/**
 * Declarative JPA EntityTransactions
 *
 * @author Gavin King
 */
@Transactional
@Interceptor
public class EntityTransactionInterceptor  implements Serializable
{

    private
    @Inject
    @Any
    EntityManager em;

    @AroundInvoke
    public Object aroundInvoke(InvocationContext ic) throws Exception
    {
        boolean act = !em.getTransaction().isActive();
        if (act)
        {
            em.getTransaction().begin();
        }
        try
        {
            Object result = ic.proceed();
            if (act)
            {
                em.getTransaction().commit();
            }
            return result;
        }
        catch (Exception e)
        {
            if (act)
            {
                em.getTransaction().rollback();
            }
            throw e;
        }
    }
}

11. Add the following Java class

package org.seamframework.tx;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@Retention(RUNTIME)
@Target({METHOD, TYPE})
@Documented
@InterceptorBinding
public @interface Transactional {}

12. Add the following Java class

package weld.example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * User: rossmyt
 * Date: 28.01.2010
 * Time: 14:02:07
 */
@Entity
public class Book
{

    private Long id;

    private String name;
    private String author;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }
    @Column(name = "name")
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
    @Column(name = "author")
    public String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }
}

13. Add the following Java class

package weld.example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.seamframework.tx.Transactional;

@Named(value = "bookFactory")

@SessionScoped

public class BookFactory implements Serializable
{
    private final String text1 = "Write your book in a sec!";
    private final String text2 = "List of written books";


    @Inject
    EntityManager em;


    private Book book = new Book();
    List<Book> books = new ArrayList<Book>();

    public String getText1()
    {
        return text1;
    }

    public void setBook(Book book)
    {
        this.book = book;
    }

    public Book getBook()
    {
        return book;
    }

    public List<Book> getBooks()
    {
        return books;
    }

    @SuppressWarnings({"unchecked"})
    @Transactional
    public void saveBook()
    {
        em.persist(book);
        books = em.createQuery("from Book").getResultList();
        book = new Book();
    }

    public String getText2()
    {
        return text2;  //To change body of created methods use File | Settings | File Templates.
    }
}

14. Add the following Java Class

package weld.example;

import java.io.Serializable;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * User: rossmyt
 * Date: 17.02.2010
 * Time: 16:41:35
 */
@Named(value = "init")
@ApplicationScoped
public class Init implements Serializable
{
    @Produces
    @ApplicationScoped
    @PersistenceContext(unitName = "foo")
    EntityManager em;
}

15. Add the file beans.xml to directory src/main/webapp/WEB-INF and edit it as follows:

<beans

        xmlns="http://java.sun.com/xml/ns/javaee"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="

      http://java.sun.com/xml/ns/javaee

      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

    <interceptors>

        <class>org.seamframework.tx.EntityTransactionInterceptor</class>

    </interceptors>

</beans>

16. Add the file faces-config.xml to directory src/main/webapp/WEB-INF and edit it as follows:

<?xml version='1.0' encoding='UTF-8'?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
</faces-config>

17. Edit the file web.xml indirectory src/main/webapp/WEB-INF as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
	  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

   <display-name>Weld Numberguess example</display-name>

   <context-param>
      <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
      <param-value>.xhtml</param-value>
   </context-param>

   <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
   </listener>

   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.jsf</url-pattern>
   </servlet-mapping>

   <session-config>
      <session-timeout>10</session-timeout>
   </session-config>

   <resource-env-ref>
      <description>Object factory for the CDI Bean Manager</description>
      <resource-env-ref-name>BeanManager</resource-env-ref-name>
      <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
   </resource-env-ref>

</web-app>

18. Add the file index.xhtml to directory src/main/webapp and edit it as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Weld and JPA Portable Extension Demo</title>
</h:head>
<h:body>
    <h1>#{bookFactory.text1}</h1>
    <h:form>
        <h:panelGrid columns="2">
            <p>Title</p>
            <h:inputText value="#{bookFactory.book.name}"/>

            <p>Author</p>
            <h:inputText value="#{bookFactory.book.author}"/>
        </h:panelGrid>
        <table>
            <ui:fragment rendered="#{not empty bookFactory.books}">
                <th><h:outputText value="#{bookFactory.text2}"/>
                </th>
                <ui:repeat var="book" value="#{bookFactory.books}">
                    <tr>
                        <td>#{book.author}</td>
                        <td>#{book.name}</td>
                    </tr>
                </ui:repeat></ui:fragment>
        </table>
        <h:commandButton action="#{bookFactory.saveBook}" value="Write it!"/>
    </h:form>
</h:body>
</html>

19. Now you can build and run the webapp:

mvn clean package tomcat:run-war

20. Access the webapp at http://localhost:8080/weldTest/index.jsf . If all went well you should be able to write a book, and it should appear in the book list after writing it.

12 comments:
 
24. Apr 2010, 23:25 America/New_York | Link
Flavio Henrique | flavio.AT.metha.com.br

It worked, but i have a question:

a) the injected EntityManager is allways in ApplicationScoped, even injecting EntityManager in a RequestScopedBean.

There is a way o change the scope during injection?

 
24. Apr 2010, 23:43 America/New_York | Link
Flavio Henrique

I changed the code to @Dependent following the CDI specs and i was able to bound the EntityManager scope to the bean it was injected! There is a problem with that approach?

... public class Init implements Serializable { ... @Dependent ... EntityManager em; }

8.1. Scope of a producer method The scope of the producer method defaults to @Dependent, and so it will be called every time the container injects this field or any other field that resolves to the same producer method.

 
10. May 2010, 22:19 America/New_York | Link
mmmmm....

Did you use this in real application code?

I'm also interested on running JPA + Weld + tomcat but as far as I understand this code makes any call transactional, isnt?
 
14. May 2010, 00:05 America/New_York | Link
Flavio Henrique wrote on Apr 24, 2010 23:25:
It worked, but i have a question: a) the injected EntityManager is allways in ApplicationScoped, even injecting EntityManager in a RequestScopedBean. There is a way o change the scope during injection?

Isnt working for me. When em is injected in EntityTransactionInterceptor, weld injects a new instance, dont use the existence one unless you use ApplicationScoped or SessionScoped in the producer. I guess it should work alike with ConversationScope, I didnt try. But @Dependent dont work even if backing bean is session scoped because an new em instance is inyected in EntityTransactionInterceptor.

From Weld reference:

An instance of a dependent bean is never shared between different clients or different injection points. It is strictly a dependent object of some other object. It is instantiated when the object it belongs to is created, and destroyed when the object it belongs to is destroyed.

 
04. Jun 2010, 04:16 America/New_York | Link

I tried the example above without using maven

my libraries are

antlr-2.7.6.jar cdi-api.jar com.springsource.javax.servlet.jsp.jstl-1.2.0.jar com.springsource.org.hsqldb-1.8.0.9.jar commons-beanutils-1.7.0.jar commons-codec-1.3.jar commons-collections-3.1.jar commons-digester-1.8.jar commons-discovery-0.4.jar commons-logging-1.1.1.jar dom4j-1.6.1.jar hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate3.jar javassist-3.9.0.GA.jar jsf-api.jar jsf-impl.jar jta-1.1.jar slf4j-api-1.5.8.jar weld-api.jar weld-core.jar weld-servlet-int.jar weld-servlet.jar weld-spi.jar weld-tomcat-support.jar

when i access the page http://localhost:8080/weldTest/index.jsf

i get the following error

org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type @ConversationScoped

at org.jboss.weld.conversation.ConversationImpl.checkConversationActive(ConversationImpl.java:79)
	at org.jboss.weld.conversation.ConversationImpl.isTransient(ConversationImpl.java:234)
	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:597)
	at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)
	at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
	at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
	at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298)
	at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)
	at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
	at org.jboss.weld.conversation.ConversationImpl_$$_javassist_2.isTransient(ConversationImpl_$$_javassist_2.java)
	at org.jboss.weld.conversation.AbstractConversationManager.cleanupConversation(AbstractConversationManager.java:148)
	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:597)
	at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)
	at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
	at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
	at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298)
	at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)
	at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)
	at org.jboss.weld.conversation.ServletConversationManager_$$_javassist_0.cleanupConversation(ServletConversationManager_$$_javassist_0.java)
	at org.jboss.weld.jsf.WeldPhaseListener.afterRenderResponse(WeldPhaseListener.java:131)
	at org.jboss.weld.jsf.WeldPhaseListener.afterPhase(WeldPhaseListener.java:103)
	at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107)
	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
	at java.lang.Thread.run(Thread.java:619)

any clues what is happening ....?

I'll really appreciate if some one can help me with this.....

 
28. Jun 2010, 23:42 America/New_York | Link

I also have just tried to create a simple CDI app to run on tomcat and get the same exception as the user directly above. I have read earlier posts about this exception (this forum) and tried some later produced weld-core jars from the repository (up to SP1) without success.

 
08. Jul 2010, 07:21 America/New_York | Link

WELD-001303 No active contexts for scope type @ConversationScoped

I am trying to get Weld / CDI running on an old ugly Weblogic 10.3.1 as i want to use CDI in the web app tier. A simple jsf bean results in the same exception.

It turned out that the WeldPhaseListener is registered twice in the jsf app. If a request completes the conversation is shut down as it should be, but then the other instance of the WeldPhaseListener run and causes the mentioned exception.

By now I do not know why there are two instances of the WeldPhaseListener registered in the app; i will go on debugging.

Any hints and remarks are welcome.

In order to investigate if there is the same root cause set a breakpoint at WeldPhaseListner.afterRenderResponse, call your page and step back in the debugger to the to

RenderResponsePhase(Phase).handleAfterPhase(FacesContext, ListIterator PhaseListener , PhaseEvent) line: 189

or set a breakpoint there.

You can now see the listeners active.

 
08. Jul 2010, 10:13 America/New_York | Link

Workaround

As mentioned in the docs, I first registered the WeldPhaseListener in the faces-config.xml, but it causes two instances, so comment it out

<!-- commment this out, if you habe two instances of WeldPhaseListener
<lifecycle>
  <phase-listener>org.jboss.weld.jsf.WeldPhaseListener</phase-listener>
 </lifecycle>
-->
 
08. Jul 2010, 10:20 America/New_York | Link

hi,

you can reduce the libs as weld-servlet.jar contains all the Weld stuff.

Concerning you Exception have a look at my post on the problem here in this topic.

 
08. Jul 2010, 10:22 America/New_York | Link
Olaf Groenemann wrote on Jul 08, 2010 10:20:
hi, you can reduce the libs as weld-servlet.jar contains all the Weld stuff. Concerning you Exception have a look at my post on the problem here in this topic.

Oops - clicked on reply instead of quote: The sentences above apply to the posts from damian and gaurav

 
09. May 2011, 11:35 America/New_York | Link
Thank you for this wonderful post.

However, may I suggest that we use archetype names instead of numbering as each of us might have different set of available archetypes?
 
28. Aug 2011, 05:46 America/New_York | Link

Some good information been shared here good stuff. Need to upgrade all my coding skills