[英]JDBC query does not see session.flush modifications on the same transaction
I have a project with Spring and Hibernate. 我有一个关于Spring和Hibernate的项目。
After an insert made with hibernate I cannot see the data in the jdbc sql even thou I've called the session.flush method. 用休眠进行插入后,即使您调用了session.flush方法,我也看不到jdbc sql中的数据。 Any idea why this is happening?
知道为什么会这样吗?
My configuration is like this: 我的配置是这样的:
<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>
I've also tried <prop key="hibernate.connection.autocommit">false</prop>
with the same result. 我也尝试过
<prop key="hibernate.connection.autocommit">false</prop>
,结果相同。
The datasource is hiraki ds but I've tried bonecp with the same result. 数据源为hiraki ds,但我尝试了相同的结果。
@Bean
public DataSource dataSource() {
final HikariDataSource dataSource = new HikariDataSource();
// dataSource.setAutoCommit(false);
dataSource.setDriverClassName(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUsername(jdbcUsername);
dataSource.setPassword(jdbcPassword);
dataSource.setIdleTimeout(60000);
dataSource.setMinimumIdle(0);
dataSource.setMaximumPoolSize(2);
return dataSource;
}
The code that I am using is: 我正在使用的代码是:
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();
}
And I do not get the rs's next value. 而且我没有得到rs的下一个价值。
If I use the doWork method the result set has values but I do not want to use it like this. 如果我使用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"));
}
}
});
I have a HibernateDAOSupport class for setting accessing datasource and sessionfactory that are set from spring configuration xml: 我有一个HibernateDAOSupport类,用于设置对从spring配置xml设置的数据源和sessionfactory的访问:
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();
}
}
And the xml: 和xml:
<bean id="hibernateDAO" class="ro.asf.capone.server.dao.HibernateDAOSupport" abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
I found a solution for this by changing the getConnection
implementation in HibernateDAOSupport
class with this: 我通过更改
HibernateDAOSupport
类中的getConnection
实现,找到了解决方案:
public Connection getConnection(){
return org.springframework.jdbc.datasource.DataSourceUtils.getConnection(dataSource);
}
Thanks M. Deinum for pointing out that a new connection is offered from the datasource and not the same one gets used. 感谢M. Deinum指出,数据源提供了一个新连接,并且使用的连接不相同。 I was under the impression that a single connection spans a transaction.
我的印象是单个连接跨越了一个事务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.