seam 2.3、ejb 3、および mysql 5.6 でアプリケーションを実行しています。現在、一括アップロード機能を試しています (csv から値を取得してデータベースに保存します)。
ここで、すべてのレコードに問題がなければ、すべてがデータベースにコミットされますが、正しくないレコードがある場合、トランザクションは失敗し、エンティティ マネージャーはデータベースへの接続を失います。この時点から、query.getResultSet()、em.persist() などの操作はすべて失敗します。
この時点からのすべてのレコードは、以下の例外 (スタックトレース) でエラーになります。userTransaction を使用して、一度に 1 つのレコードをコミットしています ... ロールバックしないようにします。
[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();
}
}