Java JDBC查询未在同一事务上看到session.flush修改
我有一个关于Spring和Hibernate的项目 在使用hibernate进行插入之后,即使调用了session.flush方法,我也无法在JDBCSQL中看到数据。知道为什么会这样吗 我的配置如下:Java JDBC查询未在同一事务上看到session.flush修改,java,hibernate,spring-orm,Java,Hibernate,Spring Orm,我有一个关于Spring和Hibernate的项目 在使用hibernate进行插入之后,即使调用了session.flush方法,我也无法在JDBCSQL中看到数据。知道为什么会这样吗 我的配置如下: <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="packagesToScan">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>ro.asf.capone.common.model</value>
</list>
</property>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${agency.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${agency.hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${agency.hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>
<property name="entityInterceptor" ref="auditInterceptor" />
</bean>
我使用的代码是:
final AuditUsers typ = new AuditUsers();
typ.setEntityId(1l);
typ.setLevel(29);
final Serializable typid = getSession().save(typ);
System.out.println(">>>>>>>>>>>>>> id: " + typid);
getSession().flush();
getSession().clear();
final String sqltyp = "select * from AUDIT_USERS where id = " + typid;
try(Connection con = getConnection(); Statement stm = con.createStatement()){
System.out.println("!!!" + con.getAutoCommit());
final ResultSet rs = stm.executeQuery(sqltyp);
while(rs.next()){
System.out.println("RS filename: " + rs.getString("ENTITY_ID"));
}
}catch (final Exception e) {
e.printStackTrace();
}
我没有得到rs的下一个值
如果我使用doWork方法,结果集有值,但我不想这样使用它
getSession().doWork(new Work() {
@Override
public void execute(final Connection connection) throws SQLException {
final Statement stm = connection.createStatement();
final ResultSet rs = stm.executeQuery(sqltyp);
while(rs.next()){
System.out.println("RS filename: " + rs.getString("ENTITY_ID"));
}
}
});
我有一个HibernateDataSupport类,用于设置从spring配置xml设置的访问数据源和会话工厂:
public abstract class HibernateDAOSupport {
private SessionFactory sessionFactory;
private DataSource dataSource;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(final DataSource dataSource) {
this.dataSource = dataSource;
}
public void setDs(final DataSource dataSource) {
this.dataSource = dataSource;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
以及xml:
<bean id="hibernateDAO" class="ro.asf.capone.server.dao.HibernateDAOSupport" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
我通过在
HibernateDAOSupport
类中更改getConnection
实现找到了一个解决方案:
public Connection getConnection(){
return org.springframework.jdbc.datasource.DataSourceUtils.getConnection(dataSource);
}
感谢M.Deinum指出,数据源提供了一个新连接,但使用的不是同一个连接。我的印象是,单个连接跨越一个事务 我找到了一个解决方案,通过将
HibernateDAOSupport
类中的getConnection
实现更改为:
public Connection getConnection(){
return org.springframework.jdbc.datasource.DataSourceUtils.getConnection(dataSource);
}
感谢M.Deinum指出,数据源提供了一个新连接,但使用的不是同一个连接。我的印象是,单个连接跨越一个事务
getConnection
将打开一个新连接,该连接是当前事务的一部分,因此不会看到更改。不要使用普通连接,而是使用JdbcTemplate
,它将使用线程绑定连接。我在hibernate4中找不到JdbcTemplate。这就是我创建HibernateDAOSupport类的原因。但我会检查新的连接备注。ThanksI在spring类中找到了JdbcTemplate。getConnection
将打开一个新连接,它是当前事务的一部分,因此看不到更改。不要使用普通连接,而是使用JdbcTemplate
,它将使用线程绑定连接。我在hibernate4中找不到JdbcTemplate。这就是我创建HibernateDAOSupport类的原因。但我会检查新的连接备注。ThanksI在spring类中找到了JdbcTemplate。