我将使用数据库和JMS队列,因此我决定使用JTATransactionManager(多种资源)。
我正在使用Spring Framework 3.2.3.RELEASE
hibernate 4.2.1。最终
玻璃鱼3.1.2.2

使用JTATransactionManager不会将任何内容提交给数据库。我尝试使用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通用道
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);
   }
 }

最佳答案

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

09-04 04:22