1

我有一个在 seam 2.3、ejb 3 和 mysql 5.6 上运行的应用程序。目前正在尝试批量上传功能(从 csv 获取值并将它们保存到数据库中)

在这里,如果所有记录都正常,则所有内容都会提交到数据库,但如果记录不正确,则事务失败并且实体管理器会失去与数据库的连接。任何像 query.getResultSet(), em.persist() 这样的操作都会从此时开始失败。

从这一点开始的所有记录都会出错,并出现以下异常(堆栈跟踪)。我正在使用 userTransaction 一次提交一条记录......这样事情就不会回滚。

[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--127.0.0.1-8080-1) SQL Error: 0, SQLState: null
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--127.0.0.1-8080-1) javax.resource.ResourceException: IJ000460: Error checking for a transaction
INFO  [stdout] (http--127.0.0.1-8080-1) get role err: org.hibernate.exception.GenericJDBCException: Could not open connection
ERROR [stderr] (http--127.0.0.1-8080-1) javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261)
...

16:15:44,275 ERROR [stderr] (http--127.0.0.1-8080-1) Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
16:15:44,279 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:52)
16:15:44,283 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
16:15:44,288 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
16:15:44,291 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
16:15:44,295 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
16:15:44,298 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHand
ler.java:82)
16:15:44,302 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.jav
a:138)
16:15:44,306 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
16:15:44,309 ERROR [stderr] (http--127.0.0.1-8080-1)    at $Proxy94.prepareStatement(Unknown Source)
16:15:44,312 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:147)
16:15:44,315 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(Statem
entPreparerImpl.java:166)
16:15:44,319 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145
)
...

16:15:44,369 ERROR [stderr] (http--127.0.0.1-8080-1) Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction
16:15:44,375 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:137)
16:15:44,380 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionP
rovider.java:67)
16:15:44,385 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSession
Impl.java:276)
16:15:44,393 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
16:15:44,412 ERROR [stderr] (http--127.0.0.1-8080-1)    ... 94 more
16:15:44,415 ERROR [stderr] (http--127.0.0.1-8080-1) Caused by: javax.resource.ResourceException: IJ000460: Error checking for a transaction
16:15:44,418 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.
java:362)
16:15:44,422 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.j
ava:464)
16:15:44,427 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:129)
16:15:44,444 ERROR [stderr] (http--127.0.0.1-8080-1)    ... 97 more
16:15:44,447 ERROR [stderr] (http--127.0.0.1-8080-1) Caused by: javax.resource.ResourceException: IJ000459: Transaction is not active: tx=TransactionImple < ac, BasicAc
tion: 0:ffff0a6a6520:25c36e83:51de8ae5:24 status: ActionStatus.ABORT_ONLY >
16:15:44,464 ERROR [stderr] (http--127.0.0.1-8080-1)    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.
java:352)

代码片段:

try
{
UserTransaction utx = Transaction.instance();
getRole(newUser, em, utx);
    commit(newUser, em, utx);
    log.info("after flush::::::::::::::::::::::::::::::");
    }catch (Exception e) {

       System.out.println("Inside Exception !!!!!!!!");
       em.clear();
}

public void getRole(User newUser, EntityManager em, UserTransaction utx)
{
    try
    {     

        Role newRole = new Role();
        System.out.println("is connection open: " + em.isOpen());
        Query roleQuery = em
            .createQuery("from Role role where role.roleId=:roleId");
        roleQuery.setParameter("roleId", 1);

        List<Role> listRole = new ArrayList<>();

        listRole = roleQuery.getResultList();

        newUser.setRole(newRole);

    } catch (Exception e) {
        System.out.println("get role err: " + e.getCause());
        e.printStackTrace();
    }


 public void commit(User newUser, EntityManager em, UserTransaction utx)
  {
  try
     {
        // em.setFlushMode(FlushModeType.COMMIT);
        System.out.println("before utx begin");
        utx.begin();
        System.out.println("before em persist");
        em.persist(newUser);
        System.out.println("before em flush");
        System.out.println("before em begin: " + utx.getStatus() + ": "
            + utx.isActive() + ": " + utx.isNoTransaction());

        em.flush();
        System.out.println("before em commit");
        utx.commit();
    } 
 }
4

0 回答 0