Results 1 to 5 of 5

Thread: @AnnotationDrivenTx

  1. #1
    Join Date
    Nov 2007
    Posts
    18

    Default @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. #2
    Join Date
    Aug 2008
    Location
    Billings, Montana
    Posts
    47

    Default

    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

  3. #3
    Join Date
    Nov 2007
    Posts
    18

    Default

    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

  4. #4
    Join Date
    Aug 2008
    Location
    Billings, Montana
    Posts
    47

    Default

    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

  5. #5
    Join Date
    Nov 2007
    Posts
    18

    Smile

    Hi Arul,

    Thanks that worked like a charm.

    -Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •