我目前在Java-Web-Application中使用Hibernate
和Wildfly
时遇到问题:
例外:
Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:116) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:180) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
... 194 more
Caused by: java.sql.SQLException: You cannot commit during a managed transaction!
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:1065)
at org.jboss.jca.adapters.jdbc.WrappedConnection.commit(WrappedConnection.java:758)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112) [hibernate-core-4.3.8.Final.jar:4.3.8.Final]
... 195 more
的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:/jdbc/salome-database</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.archive.autodetection">class</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="hibernate-mapping.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
public static SessionFactory getSessionFactory()
{
if (sessionFactory == null)
{
// loads configuration and mappings
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
EventDAO.java
public Event getById(int id)
{
List<Event> eventList = new ArrayList<Event>();
Session session = sessionFactory.getCurrentSession();
session.getTransaction().begin();
try
{
Query q = session.createQuery("from Event as event where event.id='" + id + "'");
q.setCacheable(true);
eventList = (List<Event>) q.list();
session.getTransaction().commit();
}
catch (RuntimeException e)
{
throw e;
}
finally
{
}
if (eventList.isEmpty())
{
return null;
}
else
{
return eventList.get(0);
}
}
public void addEvent(Event event)
{
Session session = sessionFactory.getCurrentSession();
session.getTransaction().begin();
try
{
session.save(event);
session.getTransaction().commit();
}
catch (RuntimeException e)
{
throw e;
}
finally
{
}
}
public void deleteEvent(Event event)
{
Session session = sessionFactory.getCurrentSession();
session.getTransaction().begin();
try
{
session.delete(event);
session.getTransaction().commit();
}
catch (RuntimeException e)
{
throw e;
}
finally
{
}
}
我已经做了很多搜索并找到了一个解决方案是删除所有Transaction
处理的线程,因为JTA会处理这个问题。当我删除transaction.commit()
语句时,我得到另一个异常,通知我不支持嵌套事务。
编辑: 我在wildfly控制台中使用以下语句创建了JNDI /数据源:
data-source add --name=salome-datasource --jndi-name=java:/jdbc/salome-database --driver-name=mysql-connector-java-5.1.35.jar_com.mysql.jdbc.Driver_5_1 --connection-url=jdbc:mysql://127.0.0.1:3306/salomeapp --user-name=*** --password=***
答案 0 :(得分:1)
您还应该删除session.getTransaction().begin();
以及session.getTransaction().commit();
。
使用容器管理的事务时,不应该自己启动事务。当你不知道自己在做什么时,容器管理的交易是你的方式。
您应该在java ee。
中阅读一些关于container managed transactions
的内容