Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
@AnnotationDrivenTx Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @AnnotationDrivenTx

    Hi All,

    I am using the lastest nightly of javaconfig (574), when I try to insert data into the database I get "javax.persistence.TransactionRequiredExceptio n: no transaction is in progress" error.

    I have the @AnnotationDrivenTx on my configuation classes that have DAO's in them and I have a TransactionManager bean named "transactionManager".

    These same DAO's worked with the XML configuration and the @Transactional annotation. I dont get any errors unitl I try to insert data.

    I am not seeing what I am doing wrong.

    Any help would be greatly appreciated.

    Thanks,
    Mike

  • #2
    Mike,

    @AnnotationDrivenTx behaves exactly like <tx:annotation-driven/>. It assumes that you have a bean named 'transactionManager' and your services are annotated with @Transactional attribute.

    Do you have something like shown below in your configuration?

    Code:
        /**
         * Transaction manager for a single JDBC DataSource (alternative to JTA)
         */
        public @Bean DataSourceTransactionManager transactionManager() {
            return new DataSourceTransactionManager(dataSource());
        }
    It would be useful if you post your configuration.

    -Arul

    Comment


    • #3
      Hi Arul,

      Thank you for the reply and any help you can offer.

      As I stated I do have a bean named transactionManager.

      Here is my config.

      Code:
      package com.project.core.dao;
      
      import java.util.Properties;
      
      import javax.persistence.EntityManagerFactory;
      import javax.sql.DataSource;
      
      import org.springframework.config.java.annotation.Bean;
      import org.springframework.config.java.annotation.Configuration;
      import org.springframework.config.java.plugin.tx.AnnotationDrivenTx;
      import org.springframework.config.java.support.ConfigurationSupport;
      import org.springframework.config.java.util.DefaultScopes;
      import org.springframework.jndi.JndiObjectFactoryBean;
      import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
      import org.springframework.orm.jpa.JpaTransactionManager;
      import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
      import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
      
      import com.project.db.dao.MyJdbcTemplate;
      
      /**
       *  * Java Configuration for Hibernate, Database and JPA
       *
       */
      @AnnotationDrivenTx
      @Configuration
      public class Config extends ConfigurationSupport {
      
      	
      	
      	/**
      	 * Standard Data Source
      	 * 
      	 * @return jndiObjectFactoryBean
      	 */
      	@Bean(scope = DefaultScopes.SINGLETON)
      	public DataSource dataSource() {
      		
      		JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
      		jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/myDB");
      		jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
      		return (DataSource) this.getObject(jndiObjectFactoryBean);
      	}
      	
      	/**
      	 * Entity Manager
      	 * 
      	 * @return entityManagerFactory
      	 */
      	@Bean(scope = DefaultScopes.SINGLETON)
      	public EntityManagerFactory entityManagerFactory() {
      		
      		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
      		entityManagerFactoryBean.setDataSource(dataSource());
      		
      		HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
      		jpaVendorAdapter.setDatabasePlatform(databaseDialect);
      		jpaVendorAdapter.setShowSql(true);
      		jpaVendorAdapter.setGenerateDdl(true);
      		
      		entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
      
      		
      		return (EntityManagerFactory) this.getObject(entityManagerFactoryBean);
      	}
      	
      	/**
      	 * Transaction Manager
      	 * 
      	 * @return transactionManager
      	 */
      	@Bean(scope = DefaultScopes.SINGLETON)
      	public JpaTransactionManager transactionManager() {
      		
      		JpaTransactionManager transactionManager = new JpaTransactionManager();
      		transactionManager.setEntityManagerFactory(entityManagerFactory());
      		transactionManager.setDataSource(dataSource());
      		
      		return transactionManager;
      	}
      
      	
      	/**
      	 * JDBC Template
      	 *  
      	 * @return jdbcTemplate
      	 */
      	@Bean(scope = DefaultScopes.SINGLETON)
      	public MyJdbcTemplate jdbcTemplate() {
      		
      		MyJdbcTemplate jdbcTemplate = new MyJdbcTemplate();
      		jdbcTemplate.setDataSource(dataSource());
      		
      		return jdbcTemplate;
      	}
      	
      	/**
      	 * Hibernate Session Factory
      	 * 
      	 * @return transactionManager
      	 */
      	@Bean(scope = DefaultScopes.SINGLETON)
      	public AnnotationSessionFactoryBean sessionFactory() {
      		
      		AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
      		
      		Properties hibernateProperties = new Properties();
      		hibernateProperties.put("hibernate.dialect", databaseDialect);
      		hibernateProperties.put("hibernate.autocommit", false);
      		
      		sessionFactory.setHibernateProperties(hibernateProperties);
      		sessionFactory.setDataSource(dataSource());
      		
      		Class[] annotatedClasses = {
      				com.project.core.model.User.class,
      				com.project.core.model.UserGroup.class,
      				com.project.core.model.Component.class,
      				com.project.core.model.ComponentGroup.class,
      				com.project.core.model.Context.class,
      				com.project.core.model.ContextUserRule.class,
      				com.project.core.model.Host.class,
      				com.project.core.model.UnixGroup.class
      		};
      		
      		sessionFactory.setAnnotatedClasses(annotatedClasses);
      		
      		return sessionFactory;
      	}
      
      	
      	private static String databaseDialect = "org.hibernate.dialect.PostgreSQLDialect";
      }
      Thanks,
      Mike

      Comment


      • #4
        Hi Mike,

        I was able to successfully CRUD using your configuration in Tomcat6/Postgres for the PetClinic sample bundled with SJC, only with a slight modification.

        I see your configuration is missing @AnnotationDrivenConfig. This annotation checks for JPA support in bean classes, and if present it adds the PersistenceAnnotationBeanPostProcessor. This annotation is available only in M4 snapshots.

        I tested in MySQL too. It worked like a charm.

        Here is my Tomcat context.xml
        Code:
        <Context>
            <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
                    useSystemClassLoaderAsParent="false"/>
            <Resource name="jdbc/petclinic" auth="Container" type="javax.sql.DataSource"
                      maxActive="10" maxIdle="5" maxWait="15000"
                      removeAbandoned="true" removeAbandonedTimeout="300" logAbandoned="true"
                      initialSize="2" minIdle="2" username="postgres" password="secret"
                      driverClassName="org.postgresql.Driver"
                      url="jdbc:postgresql://localhost:5432/petclinic"/>
        </Context>
        Here is my web.xml
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns="http://java.sun.com/xml/ns/javaee"
                 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                 id="WebApp_ID" version="2.5">
        
          <display-name>Spring JavaConfig PetClinic</display-name>
          <description>Spring PetClinic application demonstrating use of Spring JavaConfig</description>
        
          <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>petclinic.root</param-value>
          </context-param>
        
          <context-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
          </context-param>
        
          <context-param>
            <param-name>contextConfigLocation</param-name>
              <param-value>org.springframework.samples.petclinic.config.JpaConfig</param-value>
          </context-param>
        
          <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
        
          <servlet>
            <servlet-name>petclinic</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextClass</param-name>
                <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
            </init-param>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>org.springframework.samples.petclinic.config.PetclinicServletConfig</param-value>
            </init-param>
            <load-on-startup>2</load-on-startup>
          </servlet>
        
          <servlet-mapping>
            <servlet-name>petclinic</servlet-name>
            <url-pattern>/main/*</url-pattern>
          </servlet-mapping>
        
            <resource-ref>
                <description>PetClinic Database</description>
                <res-ref-name>jdbc/petclinic</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
            </resource-ref>
        
        </web-app>
        Let me know if this works for you.

        -Arul

        Comment


        • #5
          Hi Arul,

          Thanks that worked like a charm.

          -Mike

          Comment

          Working...
          X