我正在尝试使用JPA,Spring,hibernate和tomcat配置JOTM。我已经以某种方式配置但是它既不执行插入也不执行更新,换句话说,我无法在插入/更新数据后执行提交。我有以下配置。
%TOMCAT_HOME%/ CONF / context.xml中
<?xml version="1.0" encoding="utf-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/myDS" auth="Container" type="javax.sql.XADataSource" factory="org.objectweb.jotm.datasource.DataSourceFactory" driverClassName="com.mysql.jdbc.Driver" username="root" password="mypassword" url="jdbc:mysql://localhost:3306/myDB"/>
<Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"/>
<Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
</Context>
在%TOMCAT%/ lib
中提取的JOTM 2.1.9库 Jotm-datasource.jar
· Jotm-core.jar
· Xapool.jar
· Log4j.jar
· Commons-loggin-api.jar
· Carol-iiop-delegate.jar
· Carol-interceptors.jar
· Carol.jar
· Ow2-jta-1.1-spec.jar
· Ow2-connector-1.5-spec.jar
· Howl.jar
的src /主/ web应用/ META-INF / context.xml中
<Context path="/MyJPATest" reloadable="true">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
的src /主/ web应用/ WEB-INF /类/ carol.properties
# JNDI (Protocol Invocation)
carol.protocols=jrmp
# Local RMI Invocation
carol.jvm.rmi.local.call=true
# do not use CAROL JNDI wrapper
carol.start.jndi=false
# do not start a name server
carol.start.ns=false
# Naming Factory
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
META-INF / Persistence.xml
<?xml version="1.0" encoding="utf-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/myDS</jta-data-source>
<class>com.test.domain.PUser</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
的applicationContext.xml
<bean
id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property
name="persistenceUnitName"
value="myPersistenceUnit" />
</bean>
<context:annotation-config />
<jee:jndi-lookup
id="jndiUserTransaction"
jndi-name="java:comp/UserTransaction" />
<bean
id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" >
<property
name="userTransaction"
ref="jndiUserTransaction" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories
base-package="com.test"
entity-manager-factory-ref="entityManagerFactory" />
<bean
id="hibernate4ExceptionTranslator"
class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
的web.xml
<resource-ref>
<res-ref-name>jdbc/myDS</res-ref-name>
<res-type>javax.sql.XADataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
PUserService的方法
@Transactional
public List<PUser> bootstrapUsers( )
{
List<PUser> users = new ArrayList<PUser>();
try
{
for (int i = 0; i < 5; i++)
{
PUser newUser = new PUser();
newUser.setName("B_" + i);
PUser user = this.pUserRepository.save(newUser);
users.add(user);
}
}
catch (Exception e)
{
this.logger.error(e, e);
return null;
}
return users;
}
src / main / java / com.test.dao.PUserRepository(Spring-data repository)
public interface PUserRepository extends JpaRepository<PUser, Serializable>
{
PUser findByName(String username);
}
提前致谢。
答案 0 :(得分:3)
由于您使用的是JTA,因此您的persistence.xml中的事务类型应为transaction-type="JTA"
。您可以在Configuration和bootstrapping下的here上阅读更多内容。