0

Ok, this looks to be a repeated question, however, I have been searching for this over 2 days and no success. Below are the configuration details:

Annotated App Config class

@Configuration
@ComponentScan(basePackages = "com.test")
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
public class AnnotatedAppConfig {

private static final Logger _logger = Logger
        .getLogger(AnnotatedAppConfig.class);

@Bean
public DataSource dataSource() {
    // C3P0 datasource configuration
    final ComboPooledDataSource dataSource = new ComboPooledDataSource();
    try {
        dataSource.setDriverClass(ReaderUtil.getInstance()
                .getProperty(IConst.DB_DRIVER));
    } catch (PropertyVetoException e) {
        _logger.error("Error setting driver class ", e);
    }
    dataSource.setUser(ReaderUtil.getInstance().getProperty(
            IConst.DB_USER));
    dataSource.setPassword(ReaderUtil.getInstance().getProperty(
            IConst.DB_PASSWORD));
    dataSource.setJdbcUrl(ReaderUtil.getInstance().getProperty(
            IConst.DB_URL));

    _logger.info("Datasource created successfully");
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPersistenceUnitName("testunit");
    entityManagerFactoryBean.setJpaVendorAdapter(createJPAVendorAdapter());

    _logger.info("EntityManagerFactory created successfully");
    return entityManagerFactoryBean;
}

@Bean
public PlatformTransactionManager txManager() {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory()
            .getObject());
    transactionManager.setDataSource(dataSource());

    _logger.info("Transaction Manager created successfully");
    return transactionManager;
}

private HibernateJpaVendorAdapter createJPAVendorAdapter() {
    final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setGenerateDdl(false);
    jpaVendorAdapter.setDatabase(Database.MYSQL);
    jpaVendorAdapter.setDatabasePlatform(ReaderUtil.getInstance()
            .getProperty(IConst.HIBERNATE_DB_DIALECT));
    return jpaVendorAdapter;
    }
}

Annotated Service Class

@Service
@Transactional(value="txManager")
public class TestServiceImpl extends BaseServiceImpl implements ITestService {

@Autowired
private ITestDAO testDAO;

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false, value="txManager")
public Long register(final String username, final String password,
        final String name, final String address, final Integer deptId) {
    return testDAO.register(username, password, name, address, deptId);
    }
}

When ever I try to invoke the register method, then the below error is thrown (in DEBUG mode):

TransactionAspectSupport.completeTransactionAfterThrowing(534) | Completing transaction for [com.test.service.TestServiceImpl.register] after exception: javax.persistence.TransactionRequiredException: no transaction is in progress 07 Jul 2015 18:59:36,488 230371 [http-bio-9080-exec-5]:DEBUG - RuleBasedTransactionAttribute.rollbackOn(131) | Applying rules to determine whether transaction should rollback on javax.persistence.TransactionRequiredException: no transaction is in progress

I have tried all that I could find on the net, however, no luck. Any experts, please help me know what is missing in the configuration.

PLEASE NOTE: Autowiring is working fine, read transactions (SELECT queries) are working fine.

I read somewhere that @Service & @Transactional annotations do not work if applied together on the same class (which is happening in my case, TestServiceImpl has both the annotations). Is this the case? What would be the workaround in such a situation?

kaps
  • 81
  • 2
  • 9
  • txManager is defined in the AnnotatedAppConfig class, it is there in the code above. Also, tried with removing the parameters (@Transactional) from the class, but that didn't help as well. – kaps Jul 07 '15 at 14:19
  • @we-are-borg: In the logs I can see that a new transaction is getting created, however, still getting the same error AbstractPlatformTransactionManager.getTransaction(367) | Creating new transaction with name [com.test.service.TestServiceImpl.register]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; 'txManager' – kaps Jul 07 '15 at 14:24
  • Tried your suggestion, doesn't work. Same issue. – kaps Jul 07 '15 at 14:29
  • Removed the changes that you suggested and have added the logs at http://pastebin.com/E11MQygj – kaps Jul 08 '15 at 07:38
  • I add following lines to struts.xml and don't see the above exception in the logs, but still no luck :( http://stackoverflow.com/questions/22704675/struts-2-action-method-intercepting-using-spring-aop – kaps Jul 08 '15 at 11:09
  • @we-are-borg I am not able to comment in the chat room, needs 20 reputations :( – kaps Jul 08 '15 at 11:23
  • JPA Vendor adapter is not null, checked that in the Configuration class by putting sysout – kaps Jul 08 '15 at 11:31
  • Yes, tried that, doesn't work – kaps Jul 08 '15 at 12:20
  • @we-are-borg: were you able to find any issues? I am exhausted with all that I could think to resolve the issue, but not able to get over that frustrating error. – kaps Jul 09 '15 at 06:45
  • sure, i will share that with you on dropbox – kaps Jul 09 '15 at 08:16
  • I have added the project @ https://www.dropbox.com/s/7jl9nulj1elm0a0/EmpReportOnCloud.zip?dl=0. You will get the DB script under resources folder. Please change the DB username & password in env.properties (for MySQL) and log file location in log4j.xml. Rest, I think it should be deployable without any more changes. Many thanks for helping me out, this looks to be a real pain now. Look forward to your response. – kaps Jul 09 '15 at 08:44
  • Tomcat, this is just a test project so didn't change any c3p0 properties. – kaps Jul 09 '15 at 09:30
  • @we-are-borg: were you able to look at the project that I shared and find out the missing piece. – kaps Jul 13 '15 at 10:32
  • @we-are-borg: any luck? sorry to bug you repeatedly, but i need to really get around this issue. – kaps Jul 14 '15 at 10:59
  • thanks :), will wait for your solution – kaps Jul 14 '15 at 11:37
  • @we-are-borg: do you have the solution? – kaps Jul 16 '15 at 12:15
  • I can't comment on chat, could you please let me know the changes needed here OR share the project over dropbox – kaps Jul 16 '15 at 12:49
  • I have annotated the service classes with @Transactional, is it required to annotate DAO classes as well? – kaps Jul 16 '15 at 12:57
  • Let meknow when you copy the email address, I will delete the comment then. You can also delete the question, as there is no answer already. – We are Borg Jul 16 '15 at 13:04
  • I have copied the email, pls delete the comment. I will add you – kaps Jul 16 '15 at 13:10

0 Answers0