浏览 6501 次
锁定老帖子 主题:JTA使用的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-01-09
UserTransaction tx=null; try { tx =(UserTransaction);new InitialContext();.lookup("java:comp/UserTransaction");; dao.addUser(user);; tx.commit();; } catch (Exception e); { if(tx!=null); tx.rollback();; throw new Exception(e);; } 错误如下: 13:19:02,687 INFO [STDOUT] session closed 13:19:05,859 INFO [STDOUT] filter starts 13:19:06,125 INFO [Environment] Hibernate 2.1 beta 5 13:19:06,156 INFO [Environment] hibernate.properties not found 13:19:06,156 INFO [Environment] using CGLIB reflection optimizer 13:19:06,171 INFO [Configuration] Configuration resource: /hibernate.cfg.xml 13:19:06,421 INFO [Configuration] Mapping resource: User.hbm.xml 13:19:06,703 INFO [Binder] Mapping class: com.up.cip.po.User -> user 13:19:06,984 INFO [Configuration] Configured SessionFactory: null 13:19:06,984 INFO [Configuration] processing one-to-many association mappings 13:19:06,984 INFO [Configuration] processing one-to-one association property re ferences 13:19:06,984 INFO [Configuration] processing foreign key constraints 13:19:07,046 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.MySQLDialec t 13:19:07,046 INFO [SettingsFactory] Use outer join fetching: true 13:19:07,093 INFO [NamingHelper] JNDI InitialContext properties:{} 13:19:07,093 INFO [DatasourceConnectionProvider] Using datasource: java:/mysqld atasource 13:19:07,109 INFO [TransactionFactoryFactory] Transaction strategy: net.sf.hibe rnate.transaction.JTATransactionFactory 13:19:07,109 INFO [NamingHelper] JNDI InitialContext properties:{} 13:19:07,125 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not re commended); 13:19:07,125 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not re commended); 13:19:07,390 INFO [SettingsFactory] Use scrollable result sets: true 13:19:07,390 INFO [SettingsFactory] JDBC 2 max batch size: 20 13:19:07,390 INFO [SettingsFactory] echoing all SQL to stdout 13:19:07,390 INFO [SettingsFactory] Query language substitutions: {} 13:19:07,390 INFO [SettingsFactory] cache provider: net.sf.hibernate.cache.JCSC acheProvider 13:19:07,406 INFO [Configuration] instantiating and configuring caches 13:19:07,812 INFO [SessionFactoryImpl] building session factory 13:19:08,421 INFO [SessionFactoryObjectFactory] no JNDI name configured 13:19:08,609 ERROR [STDERR] java.lang.IllegalStateException: No transaction. 13:19:08,609 ERROR [STDERR] at org.jboss.tm.TxManager.rollback(TxManager.jav a:272); 13:19:08,609 ERROR [STDERR] at org.jboss.tm.usertx.client.ServerVMClientUser Transaction.rollback(ServerVMClientUserTransaction.java:134); hibernate.cfg.xml如下: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.datasource">java:/mysqldatasource</property> <property name="show_sql">true</property> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property> <property name="jta.UserTransaction">java:comp/UserTransaction</property> <property name="hibernate.jdbc.batch_size">20</property> <!-- Mapping files --> <mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration> 如果把代码换成使用hibernate的session.beginTransaction()执行没有异常,但似乎是jdbc的事务控制。不知道是什么原因,我使用的是jboss。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-01-09
tx.begin(); 哪去了?
|
|
返回顶楼 | |
发表时间:2004-01-09
不好意思,低级错误,改了后就没有上述问题了。
我现在把代码改为如下: try { tx =(UserTransaction);new InitialContext();.lookup("java:comp/UserTransaction");; tx.begin();; dao.addUser(user);; throw new Exception("haha");; } catch (Exception e); { if(tx!=null); tx.rollback();; throw new Exception(e);; } 照理应该会rollback的,但数据库表中却增加了一条记录,并没有rollback不知道是为什么? |
|
返回顶楼 | |
发表时间:2004-01-09
为什么会这样啊?真奇怪啊!
|
|
返回顶楼 | |
发表时间:2004-01-09
dao中有session.flush()。我又试了一下,还是一样,调试日志中确定已经执行了rollback但没有将前面的数据库操作取消,为什么呢?
|
|
返回顶楼 | |
发表时间:2004-01-09
看到你用的是MySQLDialect,Mysql默认d的Table Type是不支持Transaction的,会不会是这个原因?
|
|
返回顶楼 | |
发表时间:2004-01-09
有可能,谢了,去试试看!!
|
|
返回顶楼 | |
发表时间:2004-01-09
没问题了,改成innodb的表就行了。
|
|
返回顶楼 | |