Announcement Announcement Module
Collapse
No announcement yet.
Transaction failure when HibernateDaoSupport is proxified Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction failure when HibernateDaoSupport is proxified

    Hi,

    Curiously, since I decided to proxify (AOP) my DAO (in order to convert some exceptions), I've got exceptions when calling HibernateTemplate methods:
    org.springframework.orm.hibernate3.HibernateSystem Exception: get is not valid without active transaction; nested exception is org.hibernate.HibernateException: get is not valid without active transaction
    Here is the beginning of my DAO class:
    Code:
    public final class HibernateTemplateDao extends HibernateDaoSupport implements GenericDao {
    
    	public HibernateTemplateDao(SessionFactory sessionFactory) {
    		setSessionFactory(sessionFactory);
    		getHibernateTemplate().setAllowCreate(false);
    		getHibernateTemplate().setCacheQueries(true);
    		getHibernateTemplate().afterPropertiesSet();
    	}
    
    	@Transactional(readOnly = true) @Override public <M extends AbstractModel> M read(Class<M> modelClass, Serializable id) {
    		return modelClass.cast(getHibernateTemplate().get(modelClass, id));
    	}
    This Javaconfig method works well:
    Code:
    @Bean public GenericDao genericDao() {
    	return new HibernateTemplateDao(sessionFactory());
    }
    New Javaconfig configuration (doesn't work);
    Code:
    @Bean public GenericDao genericDao() {
    	ProxyFactoryBean genericDaoProxyFactoryBean = new ProxyFactoryBean();
    	genericDaoProxyFactoryBean.setTarget(new HibernateTemplateDao(sessionFactory()));
    	genericDaoProxyFactoryBean.addAdvice(new GenericDaoProxy());
    	return (GenericDao) genericDaoProxyFactoryBean.getObject();
    }
    Proxy:
    Code:
    public final class GenericDaoProxy implements ThrowsAdvice {
    	public void afterThrowing(Exception e) {
    		System.err.println(e.toString());
    	}
    }
    Just in case, here is the rest of the DB configuration:
    Code:
    @Bean public HibernateTransactionManager transactionManager() {
    	return new HibernateTransactionManager(sessionFactory());
    }
    
    @Bean public SessionFactory sessionFactory() {
    	ComboPooledDataSource dataSource = new ComboPooledDataSource();
    	try {
    		dataSource.setDriverClass(Driver.class.getName());
    	} catch (PropertyVetoException e) {
    		throw new UnexpectedException(e);
    	}
    	dataSource.setJdbcUrl(dbUrlProperty());
    	dataSource.setPassword(dbPasswordProperty());
    	dataSource.setUser(dbUserProperty());
    	dataSource.setInitialPoolSize(DB_POOL_MIN_SIZE);
    	dataSource.setMaxConnectionAge(0);
    	dataSource.setMaxIdleTimeExcessConnections(0);
    	dataSource.setAutomaticTestTable(null);
    	try {
    		dataSource.setConnectionTesterClassName(DefaultConnectionTester.class.getName());
    	} catch (PropertyVetoException e) {
    		throw new UnexpectedException(e);
    	}
    	dataSource.setPreferredTestQuery(null);
    	dataSource.setTestConnectionOnCheckin(false);
    	dataSource.setTestConnectionOnCheckout(false);
    	dataSource.setMaxStatementsPerConnection(0);
    	dataSource.setAcquireRetryAttempts(DB_CONNECTION_ACQUIRE_RETRY_ATTEMPS);
    	dataSource.setAcquireRetryDelay(DB_CONNECTION_ACQUIRE_RETRY_DELAY);
    	dataSource.setBreakAfterAcquireFailure(false);
    	dataSource.setConnectionCustomizerClassName(null);
    	dataSource.setAutoCommitOnClose(false);
    	dataSource.setForceIgnoreUnresolvedTransactions(false);
    	dataSource.setDebugUnreturnedConnectionStackTraces(false);
    	dataSource.setUnreturnedConnectionTimeout(0);
    	dataSource.setCheckoutTimeout(0);
    	dataSource.setFactoryClassLocation(null);
    	dataSource.setMaxAdministrativeTaskTime(0);
    	dataSource.setUsesTraditionalReflectiveProxies(false);
    
    	AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    	sessionFactoryBean.setDataSource(dataSource);
    	sessionFactoryBean.setPackagesToScan(new String[] { "my.comp.app.data.model" });
    	Properties hibernateProperties = new Properties();
    	hibernateProperties.setProperty("hibernate.dialect", PostgreSQLDialect.class.getName());
    	hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
    	hibernateProperties.setProperty("hibernate.generate_statistics", String.valueOf(dbDebugProperty()));
    	hibernateProperties.setProperty("hibernate.cache.use_structured_entries", String.valueOf(dbDebugProperty()));
    	hibernateProperties.setProperty("hibernate.show_sql", String.valueOf(false));
    	hibernateProperties.setProperty("hibernate.format_sql", String.valueOf(false));
    	hibernateProperties.setProperty("hibernate.use_sql_comments", String.valueOf(dbDebugProperty()));
    	hibernateProperties.setProperty("hibernate.cache.provider_class", SingletonEhCacheProvider.class.getName());
    	hibernateProperties.setProperty("hibernate.cache.use_query_cache", String.valueOf(true));
    	hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", String.valueOf(true));
    	hibernateProperties.setProperty("hibernate.cache.use_structured_entries", String.valueOf(dbDebugProperty()));
    	hibernateProperties.setProperty("hibernate.connection.autocommit", String.valueOf(false));
    	hibernateProperties.setProperty("hibernate.connection.release_mode", "on_close");
    	hibernateProperties.setProperty("hibernate.connection.provider_class", C3P0ConnectionProvider.class.getName());
    	hibernateProperties.setProperty("hibernate.c3p0.acquire_increment", String.valueOf(DB_POOL_ACQUIRE_INCREMENT)); // acquireIncrement
    	hibernateProperties.setProperty("hibernate.c3p0.idle_test_period", String.valueOf(0)); // idleConnectionTestPeriod
    	hibernateProperties.setProperty("hibernate.c3p0.timeout", String.valueOf(0)); // maxIdleTime
    	hibernateProperties.setProperty("hibernate.c3p0.max_size", String.valueOf(DB_POOL_MAX_SIZE)); // maxPoolSize
    	hibernateProperties.setProperty("hibernate.c3p0.max_statements", String.valueOf(0)); // maxStatements
    	hibernateProperties.setProperty("hibernate.c3p0.min_size", String.valueOf(DB_POOL_MIN_SIZE)); // minPoolSize
    	hibernateProperties.setProperty("hibernate.current_session_context_class", "thread");
    	hibernateProperties.setProperty("hibernate.transaction.factory_class", SpringTransactionFactory.class.getName());
    	sessionFactoryBean.setHibernateProperties(hibernateProperties);
    	Map<String, ValidateEventListener> eventListeners = new HashMap<String, ValidateEventListener>();
    	ModelValidatorEventListener validateListener = new ModelValidatorEventListener();
    	eventListeners.put("pre-update", validateListener);
    	eventListeners.put("pre-insert", validateListener);
    	sessionFactoryBean.setEventListeners(eventListeners);
    	return (SessionFactory) getConfigured(sessionFactoryBean).getObject();
    }
    Why do I get such an exception, with just a proxy? Do you see any reason? Thanks.

  • #2
    Maybe it is a conflit between my proxy and the transaction manager. So the transaction manager is deactivated, and it does not open a valid transaction. In this case, what I should I do?

    Comment

    Working...
    X