簡體   English   中英

JDBC查詢在同一事務上看不到session.flush修改

[英]JDBC query does not see session.flush modifications on the same transaction

我有一個關於Spring和Hibernate的項目。

用休眠進行插入后,即使您調用了session.flush方法,我也看不到jdbc sql中的數據。 知道為什么會這樣嗎?

我的配置是這樣的:

 <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>

我也嘗試過<prop key="hibernate.connection.autocommit">false</prop> ,結果相同。

數據源為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;
}

我正在使用的代碼是:

 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"));
                    }
                }
            });

我有一個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();
    }
}

和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指出,數據源提供了一個新連接,並且使用的連接不相同。 我的印象是單個連接跨越了一個事務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM