获取“事务未成功启动”

时间:2018-07-02 04:02:57

标签: java hibernate tapestry

每次插入/更新到DB时,我都会收到此警告。但是,我的数据仍然保存。尽管如此,我还是想从我的日志文件中删除它。

下面是我的日志文件:

[WARN] TapestryIOCModule.PerthreadManager Error invoking listener org.apache.tapestry5.internal.hibernate.HibernateSessionManagerImpl@41c045: Transaction not successfully started org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:179) at org.apache.tapestry5.internal.hibernate.HibernateSessionManagerImpl.threadDidCleanup(HibernateSessionManagerImpl.java:65)
at org.apache.tapestry5.ioc.internal.services.PerthreadManagerImpl.cleanup(PerthreadManagerImpl.java:128)
at org.apache.tapestry5.ioc.internal.RegistryImpl.cleanupThread(RegistryImpl.java:445)
at org.apache.tapestry5.ioc.internal.RegistryWrapper.cleanupThread(RegistryWrapper.java:38)
at org.apache.tapestry5.TapestryFilter.doFilter(TapestryFilter.java:178)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

这是我的服务提示:

private HibernateSessionManager sessionManager;

public HibernateSessionManager getSessionManager() {
    return sessionManager;
}

public void setSessionManager(HibernateSessionManager sessionManager) {
    this.sessionManager = sessionManager;
}

@Inject
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public Session getHibernateSession() {
    if (getSessionManager() != null)
        return getSessionManager().getSession();
    else
        return sessionFactory.getCurrentSession();
}

@SuppressWarnings("rawtypes")
public Criteria createCriteria(Class clazz, Criterion... criterion) {
    Session session = getHibernateSession();

    if (!session.getTransaction().isActive())
        session.beginTransaction();

    Criteria crit = session.createCriteria(clazz);
    for (Criterion c : criterion)
        crit.add(c);

    return crit;    
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, ID extends Serializable> T get(ID id, boolean lock, Class clazz) {
    if (id == null)
        return null;

    T entity;
    Session session = getHibernateSession();

    if (!session.getTransaction().isActive())
        session.beginTransaction();

    if (lock)
        entity = (T) session.load(clazz, id);
    else
        entity = (T) session.get(clazz, id);

    return entity;
}

@SuppressWarnings("rawtypes")
public <T extends Serializable> List<T> findAll(Class clazz) {
    return findByCriteria(clazz);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public <T extends Serializable> List<T> findByCriteria(Class clazz, Criterion... criterion) {
    Session session = getHibernateSession();
    Criteria crit = session.createCriteria(clazz);
    for (Criterion c : criterion)
        crit.add(c);

    return crit.list();
}

public <T extends Serializable> T create(T entity) {
    Transaction tx = getHibernateSession().beginTransaction();
    getHibernateSession().saveOrUpdate(entity);
    tx.commit();
    return entity;
}

public <T extends Serializable> T update(T entity) {
    Transaction tx = getHibernateSession().beginTransaction();
    getHibernateSession().saveOrUpdate(entity);
    tx.commit();
    return entity;
}

public <T extends Serializable> void delete(T entity) {
    Transaction tx = getHibernateSession().beginTransaction();
    getHibernateSession().delete(entity);
    tx.commit();
}

public <T extends Serializable> void createOrUpdate(T entity) {
    Transaction tx = getHibernateSession().beginTransaction();
    getHibernateSession().saveOrUpdate(entity);
    tx.commit();
}

public <T extends Serializable> T merge(T entity) {
    Transaction tx = getHibernateSession().beginTransaction();
    getHibernateSession().merge(entity);
    tx.commit();
    return entity;
}

// USER

@Override
public User createOrUpdateUser(User user) {
    if (user.getId() == null || user.getId() == 0) {
        String encodePass = PasswordUtils.EncodePassword(user.getName(), user.getPassword());
        user.setPassword(encodePass);
    }

    if (user.getId() == null)
        return create(user);
    else
        return update(user);
}

@Override
public User getUserByID(Integer id) {
    return get(id, false, User.class);
}

我不知道这是否会影响我的应用程序性能。来自Stack Overflow的另一个问题相同的答案并没有帮助解决我的问题。我想知道我的情况是否不同。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用@CommitAfter注释,而不是在代码中显式启动和提交转换?