@PostConstruct首次执行失败

时间:2014-05-17 05:15:08

标签: java jsf jpa ejb cdi

由于我是使用Java EE 7的新手,因此我创建了一个仅用于研究目的的项目。 我创建了一个带有Request范围的CDI bean,如下所示(它只是实现了Serializable,因为我已尝试将它与@javax.faces.view.ViewScoped一起使用)

import com.issuerepository.domain.Issue;
import com.issuerepository.domain.IssueType;
import com.issuerepository.facade.api.IssueRepositoryFacade;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;

@Named
@RequestScoped
public class CreateIssueBean implements Serializable {

    @Inject
    private IssueRepositoryFacade issueRepository;

    private Issue issue;
    private List<IssueType> allIssueType;

    @PostConstruct
    private void init() {
        issue = new Issue();
        allIssueType = issueRepository.findAllIssueType();
    }

    public String saveIssue() {
        issueRepository.saveOrUpdateIssue(issue);
        return "searchIssue.xhtml?faces-redirect=true";
    }

    public Issue getIssue() {
        return issue;
    }

    public void setIssue(Issue issue) {
        this.issue = issue;
    }

    public List<IssueType> getAllIssueType() {
        return allIssueType;
    }

    public void setAllIssueType(List<IssueType> allIssueType) {
        this.allIssueType = allIssueType;
    }
}

问题是:我启动WildFly(A.k.a. JBoss AS 8)并且我的应用程序正常启动。然后我打开浏览器并尝试访问应用程序的URL。在第一次访问中,我得到了这个例外:

Stack Trace
javax.servlet.ServletException: WELD-000049: Unable to invoke private void com.issuemanager.web.bean.CreateIssueBean.init() on com.issuemanager.web.bean.CreateIssueBean@5c0dc28a

以上内容在浏览器上显示,以下内容显示在server.log上:

Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:63) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:162) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1884) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:909) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2553) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2539) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2364) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    ... 177 more
Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:154)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    ... 198 more
Caused by: javax.resource.ResourceException: IJ000460: Error checking for a transaction
    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:416)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:513)
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)
    ... 201 more
Caused by: javax.resource.ResourceException: IJ000459: Transaction is not active: tx=TransactionImple < ac, BasicAction: 0:ffffc0a81906:-758bf077:5376ec7d:9 status: ActionStatus.ABORT_ONLY >
    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:400)
    ... 203 more

因为您可以看到调用@PostConstruct方法时出现问题。只是你知道

IssueRepositoryFacade

是一个EJB。但是,当我刷新相同的URL时,它可以正常工作。我不知道发生了什么。此EJB使用

进行注释
@TransactionAttribute(TransactionAttributeType.REQUIRED)

所以我正在使用容器管理的事务。我使用JPA 2.1和MySql数据库。 有人,如果没有给我一个解决方案,至少可以解释为什么会这样吗?

感谢。

2 个答案:

答案 0 :(得分:0)

默认情况下,CDI Bean没有事务功能。在JTA 1.2(EE 7的一部分)中,添加了一个事务拦截器,允许将CDI bean标记为事务性。您可以尝试将PostConstruct方法标记为@Transactional(TxType.REQUIRED)

答案 1 :(得分:0)

修复了它在Wildfly数据源中禁用JTA并在persistence.xml中使用RESOURCE-LOCAL事务的问题。