JTATransactionManager不承诺数据库

时间:2013-05-30 15:04:06

标签: spring hibernate glassfish-3 spring-transactions

我将使用数据库和JMS队列,所以我决定使用JTATransactionManager(多个资源)。
我正在使用Spring Framework 3.2.3.RELEASE
Hibernate 4.2.1.Final
Glassfish 3.1.2.2

使用JTATransactionManager没有提交给db.I尝试了JPATransactionManager它工作正常。我没有得到JTATransactionManager的任何异常。我知道我做错了什么?谢谢。

这是一段jpa配置文件

<jee:jndi-lookup id="dataSource" jndi-name="Test" />

<bean id="entityManagerFactory"
   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
       p:packagesToScan="com.pinkshield.jpaTest.domain" 
       p:dataSource-ref="dataSource"
       p:jpaPropertyMap-ref="jpaPropertyMap" 
       p:jpaVendorAdapter-ref="hibernateVendor" />


<util:map id="jpaPropertyMap">
    <entry key="hibernate.hbm2ddl.auto" value="validate" />
    <entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
    <entry key="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />
    <entry key="transaction.manager_lookup_class"
        value="org.hibernate.transaction.SunONETransactionManagerLookup" />
</util:map>

<bean id="hibernateVendor"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    p:showSql="true" />

<tx:jta-transaction-manager />

<context:component-scan base-package="com.pinkshield.jpaTest" />

这是我的JPA Generic Dao

package com.pinkshield.jpaTest;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class GenericDaoJpa<T> implements GenericDao<T> 
{

private Class<T> queryClass;
protected EntityManager entityManager;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) 
{
    this.entityManager = entityManager;
}   

public GenericDaoJpa(Class<T> queryClass) 
{
    super();
    this.queryClass = queryClass;
}

public T getNewInstance() 
{
    try 
    {
        return getQueryClass().newInstance();
    } 
    catch (InstantiationException e) 
    {
        throw new RuntimeException("Error creating new instance of : " + getQueryClass().getName(), e);
    } 
    catch (IllegalAccessException e) 
    {
        throw new RuntimeException("Error creating new instance of : " + getQueryClass().getName(), e);
    }
}

public Class<T> getQueryClass() 
{
    return queryClass;
}    

public T get(Long id) 
{
    if (id == null) 
    {
        return null;
    } 
    else 
    {
        return entityManager.find(queryClass, id);
    }
}

@SuppressWarnings("unchecked")
public List<T> getAll() 
{
    return entityManager.createQuery("select o from " + queryClass.getName() + " o").getResultList();
}

public void save(T object) 
{
    entityManager.persist(object);
}

public void update(T object) 
{
    entityManager.merge(object);
}

public void delete(T object) 
{
    entityManager.remove(entityManager.merge(object));
}

}

这是UserDao

@Repository
public class UserDao extends GenericDaoJpa<User> 
{
public UserDao() 
{
    super(User.class);
}
}

这是我的服务代码

 @Service
 public class UserServiceImpl implements IUserService{

@Autowired UserDao userDao;

@Override
@Transactional
public void saveUser(String name, String lastName)
  { 
    User user=new User();
    user.setLastName(lastName);
    user.setName(name);

    userDao.save(user);
   }
 }

1 个答案:

答案 0 :(得分:2)

我认为您需要将<tx:annotation-driven/>添加到上下文xml中。这将运行一个上下文后处理器,它将使用@Transactional的方法包装一个AOP方法拦截器,该拦截器提供您正在寻找的事务行为。