Hi,
it's my first project using spring and I'm trying to get this run for hours. I'm using JSF 2, Hibernate, MySql5 and Spring 3.0.2. on a Glassfish Server 3. After setting up my configuration, I get the message "The chosen transaction strategy requires access to the JTA TransactionManager" if I want to deploy.
I have 3 tiers (presentation, service, dao) in an simple example, but I cannot get this running.
Here is my configuration:
persistence.xml
Code:<?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="WebSpringPU" transaction-type="JTA"> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="false" /> <property name="hibernate.connection.autocommit" value="false" /> <property name="hibernate.cache.use_query_cache" value="false" /> <property name="hibernate.cache.use_second_level_cache" value="false" /> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>
spring-config.xml:
As you can see there is an property in the spring-config.xml which is deactivated, where I tried to get acces to the JTA Trabsactioin Manager. If I do so, the project is deployed, but transactions are not working.Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Configure view scope --> <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="view"> <bean class="spring.ViewScope"/> </entry> </map> </property> </bean> <context:component-scan base-package="gui" /> <context:component-scan base-package="dao" /> <context:component-scan base-package="service" /> <!-- Enable processing of @PersistenceContext and @PersistenceUnit --> <context:annotation-config /> <!-- Configure a pooled data source --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/infosys12" /> <property name="username" value="service" /> <property name="password" value="123456" /> </bean> <!-- Configure the JPA entity manager factory with Hibernate --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="database" value="HSQL" /> <property name="generateDdl" value="false" /> </bean> </property> <property name="persistenceUnitName" value="WebSpringPU" /> <!-- access to the JTA TransactionManager <property name="jpaProperties"> <props> <prop key="hibernate.transaction.manager_lookup_class"> org.hibernate.transaction.SunONETransactionManagerLookup </prop> </props> </property> --> </bean> <!-- Enable transaction configuration with @Transactional, with Spring 3.0.2. you need to add com.springsource.org.aopalliance-1.0.0.jar to your libraries (it's missing) --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- Configure transaction manager for JPA --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans>
Here are some code snippets of my project:
Page1.java:
LaenderServiceImpl.java:Code:package gui; import dao.LaenderDao; import domain.Laender; import java.io.Serializable; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Named; import org.springframework.context.annotation.Scope; @Named @Scope("view") public class Page1 implements Serializable { private String value; private Laender land; @Inject LaenderDao laenderService; @PostConstruct public void construct() { System.out.println("************* contruct " + this.getClass().getName()); value = "Wert von Page 1"; try { System.out.println("Read from db..."); land=laenderService.read(); System.out.println("Old value: \""+land+"\""); System.out.println("Change value..."); land.setBezeichnung("Deutschland1"); laenderService.save(land); } catch (Exception ex) { System.out.print("ERROR: "); System.out.println(ex.getMessage()); System.out.println(ex.getClass() ); System.out.println(ex.getCause()); } } @PreDestroy public void destruct() { System.out.println("************* destroy " + this.getClass().getName()); } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Laender getLand() { return land; } public void setLand(Laender land) { this.land = land; } }
Code:package service.impl; import dao.LaenderDao; import domain.Laender; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; import org.springframework.transaction.annotation.Transactional; import service.LaenderService; @Named("laenderService") @Singleton public class LaenderServiceImpl implements LaenderService { @Inject LaenderDao laenderDao; @Transactional @Override public void save(Laender land) { laenderDao.save(land); } @Transactional @Override public Laender read() { return laenderDao.read(); } }
LaenderDaoJpa.java:
Please someone do me a favour and give me a hint, what I'm doing wrong. I'm quite sure the problem is the spring-configuration and not the java code itself. The complete project can be found as attachment (Netbeans).Code:package dao.jpa; import dao.LaenderDao; import domain.Laender; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Named; import javax.inject.Singleton; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceUnit; import javax.persistence.Query; @Named("laenderDao") @Singleton public class LaenderDaoJpa implements LaenderDao { @PersistenceContext(unitName="WebSpringPU") private EntityManager em; @Override public void save(Laender land) { System.out.println("About to persist..."); em.persist(land); System.out.println("OK!"); } @Override public Laender read() { Laender result = null; Query qry; qry = em.createNamedQuery("Laender.findById"); qry.setParameter("param", 1); result = (Laender) qry.getSingleResult(); return result; } }
Thank for your help
Tom
WebSpring.zip


Reply With Quote