如题,,
如何在glassfish中使用hibernate jta事务控制呢?
我搞了半天了,不报错,但是就是插不了数据库

解决方案 »

  1.   

    这个跟用哪个服务器没有关系的,八成是你配置搞错了,或者说sql语句没有提交惹得祸
      

  2.   

    配置代码和java代码如下,帮忙看看呢:<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration> <session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">
    jdbc:mysql://localhost/soundcard
    </property>
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">mysql</property>
    <property name="connection.password">root</property>
    <property name="connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.transaction.manager_lookup_class">
    org.hibernate.transaction.SunONETransactionManagerLookup
    </property>

    <property name="hibernate.current_session_context_class">org.hibernate.context.JTASessionContext</property>

    <property name="jta.UserTransaction">java:comp/UserTransaction</property>
    <property name="hibernate.transaction.factory_class">
    org.hibernate.transaction.JTATransactionFactory
    </property>

    <property name="show_sql">true</property>

    <!--  

    -->
    <mapping resource="com/soundcard/bean/Role.hbm.xml" />
    <mapping resource="com/soundcard/bean/Log.hbm.xml" />
    <mapping resource="com/soundcard/bean/Communication.hbm.xml" />
    <mapping resource="com/soundcard/bean/User.hbm.xml" />
    <mapping resource="com/soundcard/bean/Permission.hbm.xml" />
    <mapping resource="com/soundcard/bean/UserRole.hbm.xml" />
    <mapping resource="com/soundcard/bean/Blacklist.hbm.xml" />
    <mapping resource="com/soundcard/bean/RolePermission.hbm.xml" /> </session-factory></hibernate-configuration>
    User user = new User();
    user.setUserid(userid);
    user.setPassword(Utils.toMD5Normal(password));
    user.setName(username);
    user.setPhone(phone);

    UserRoleId userRoleId = new UserRoleId();
    userRoleId.setRoleid(roleid);
    userRoleId.setUserid(userid);

    UserRole userRole = new UserRole();
    userRole.setId(userRoleId);

    UserTransaction tx = null;

    try {
    tx = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
    } catch (NamingException e) {

    e.printStackTrace();
    }

    try {
    tx.begin();
    } catch (NotSupportedException e1) {

    e1.printStackTrace();
    } catch (SystemException e1) {

    e1.printStackTrace();
    }

    IUserDao userDao = new UserDAO();
    try {
    userDao.save(user);
    }catch(Exception e) {
    try {
    tx.rollback();
    } catch(Exception re) {
    re.printStackTrace();
    }
    }

    IUserRoleDao userRoleDao = new UserRoleDAO();
    try {
    userRoleDao.save(userRole);
    } catch(Exception e) {
    try {
    tx.rollback();
    } catch(Exception re) {
    re.printStackTrace();
    }
    }

    try {
    tx.commit();
    } catch (Exception e) {
    e.printStackTrace();

      

  3.   

    运行以后SQL语句能在服务器日志上显示出来的,但是就是插不了数据库