首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 其他服务器 >

hibernate使用jboss5下的xa数据源出现类型转换错误

2012-02-26 
hibernate使用jboss5下的xa数据源出现类型转换异常小弟最近在看hibernate书籍的时候发现书本提及了jta分布

hibernate使用jboss5下的xa数据源出现类型转换异常
小弟最近在看hibernate书籍的时候发现书本提及了jta分布式的事务处理,于是想尝试一下。我下了jboss5.1,在我机子上D:\java_data\jboss\jboss-5.1.0.GA\server\default\deploy下有一文件mysql-xa-ds.xml,内容如下

XML code
<?xml version="1.0" encoding="UTF-8"?><datasources><xa-datasource><jndi-name>MySqlXADS</jndi-name><xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class><xa-datasource-property name="Url">jdbc:mysql://127.0.0.1:3306/test</xa-datasource-property><xa-datasource-property name="User">root</xa-datasource-property><xa-datasource-property name="Password">123</xa-datasource-property><exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name><metadata>   <type-mapping>mySQL</type-mapping></metadata></xa-datasource></datasources>


然后建了个工程,在src下的hibernate.cfg.xml配置如下
XML code
<!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory name="jboss_first">        <property name="show_sql">true</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <property name="hibernate.connection.datasource">java:/MySqlXADS</property>        <mapping resource="com/jboss/domain/Student.hbm.xml"/>        <mapping resource="com/jboss/domain/Teacher.hbm.xml"/>    </session-factory></hibernate-configuration>


然后,写了个jsp测试一下
HTML code
<%@page contentType="text/html;charset=utf-8"%><%@page import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*,org.hibernate.*"%><%@page import="org.hibernate.cfg.Configuration"%><%@page import="com.jboss.domain.*" %><%SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();Session session1 = null;try{session1 = sessionFactory.openSession();session1.getTransaction().begin();Teacher teacher = new Teacher();teacher.setName("利利");session1.save(teacher);session1.getTransaction().commit();}catch(Exception e){session1.getTransaction().rollback();System.out.println(e);}finally{session1.close();}%>


部署到jboss,然后运行一下,结果成功保存了teacher的信息。
好了,这是用原生hibernate api进行的操作,而且这跟平时我们用普通的数据源都差不多的,现在,我想
利用jta进行编码,于是我把src下的 hibernate.cfg.xml配置文件修改了一下,变成
XML code
<!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory name="jboss_first">        <property name="show_sql">true</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>        <property name="hibernate.connection.datasource">java:/MySqlXADS</property>        <mapping resource="com/jboss/domain/Student.hbm.xml"/>        <mapping resource="com/jboss/domain/Teacher.hbm.xml"/>    </session-factory></hibernate-configuration> 


就是添加了两个property而已,然后写了一个jsp文件
HTML code
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%><%@ page import="javax.naming.*" %><%@page import="javax.transaction.UserTransaction"%><%UserTransaction utx = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction"); %>


部署后一运行就出错了
11:14:02,437 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: com.arjuna.ats.jbossatx.jta.TransactionManagerDelegate cannot be cast to javax.transaction.TransactionManager
at org.hibernate.transaction.JNDITransactionManagerLookup.getTransactionManager(JNDITransactionManagerLookup.java:57)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:367)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.apache.jsp.hibernate_005fmysql_005fxa_jsp._jspService(hibernate_005fmysql_005fxa_jsp.java:66)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
看了jboss官方文档并且下了jboss in action电子书查阅后还是解决不了(貌似没有讲到这方面的知识,奇怪!),请各位前辈们帮忙解决一下,不胜感激。



[解决办法]
刚刚看了一篇文章http://www.datadisk.co.uk/html_docs/java_app/jboss5/jboss5_deployment.htm
不知有用不
[解决办法]
好像是数据源在管你的事务,现在你又要hibernate来管。有冲突。
------解决方案--------------------


<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>

一个是JTA一个是JBOSS,自然不行啦
[解决办法]
把你应用下的jta.jar删掉即可

j2ee服务器本身就有 javax.transaction 包
[解决办法]
没想到这个冲突还会导致这种错误,让我也调试了半天。
用开源的东西就是麻烦啊~

热点排行