我在Spring中的事務沒有啓動時出現問題,因此對數據庫所做的更改未保存。配置如下:事務未啓動Spring + Hibernate + MySQL
web.xml文件(沒有的DispatcherServlet,有FacesServlet的)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
從applicationContext.xml中相關的配置是這樣的:
<context:annotation-config />
<context:component-scan base-package="pl.web.beans,pl.csci.dao" />
<bean id="sessionFactoryCsci" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="configLocation" value="classpath:csci_database.cfg.xml" />
</bean>
<bean id="hibernateTemplateCsci" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactoryCsci" />
</property>
</bean>
<bean id="hibernateDaoSupportCsci" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true">
<property name="hibernateTemplate" ref="hibernateTemplateCsci" />
</bean>
<bean id="csciPermSystemDAO" class="pl.csci.dao.CsciPermSystemDAO" parent="hibernateDaoSupportCsci">
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactoryCsci" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
最後類負責用於保存實體:
pl.csci.dao
@Transactional(readOnly = true)
public class CsciPermSystemDAO extends HibernateDaoSupport implements ICsciPermSystemDAO {
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public Integer addPermission(Permissions Permission) throws DaoException {
try {
Integer permissionId = (Integer) getHibernateTemplate().save(Permission);
LOGGER.info("Is transaction active?:" + DebugUtils.transactionActive());
return permissionId;
} catch (Throwable ex) {
// exception handling
}
}
上面的代碼片段(Deb ugUtils.transactionActive())返回false。此方法使用TransactionSynchronizationManager類來確定事務是否處於活動狀態。
調試時休眠,我可以看到插入記錄,但這些刀片實際上可以永遠不會出現在數據庫:
2014年1月30日15時13分15秒DEBUG [hibernate.SQL] - 插入csci_perm。權限(BRANCH,GROUP_ID,PERSON_ID,READ_PERM,SYSTEM_ID,WRITE_PERM)值(?,?,?,?,?,?)
請問我的配置有什麼問題?
最後,這裏是Hibernate的配置:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/csci_perm</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">3000</property>
<!-- seconds -->
<property name="c3p0.max_size">20</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.min_size">3</property>
<property name="c3p0.timeout">100</property>
<!-- seconds -->
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout - You can disable this once you have it working -->
<property name="show_sql">true</property>
<property name="hibernate.jdbc.batch_size">10</property>
<mapping class="pl.csci.model.Permissions"/>
</session-factory>
</hibernate-configuration>
你如何定義你的TransactionManager? – WeMakeSoftware
我編輯了第一篇文章 – Jarek