亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了H2訪問數(shù)據(jù)庫以從單獨的線程訪問測試數(shù)據(jù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的集成測試場景:

    在H2數(shù)據(jù)庫中創(chuàng)建行
    sleep(50000ms)(同時,由于Spring配置,將調(diào)用另一個線程,此線程應找到點1中創(chuàng)建的行,并更新此行)
    預期從點%1開始有行。已由點%2中提到的線程更新。

此方案同時測試配置和實現(xiàn)。這就是我想要實現(xiàn)的目標。

我在所有測試中都使用H2數(shù)據(jù)庫,因此決定在這里也使用它。在調(diào)試測試場景時,我發(fā)現(xiàn)sleep期間調(diào)用的新線程連接到數(shù)據(jù)庫,但沒有找到創(chuàng)建的行。我瀏覽了H2文檔,并開始使用:

java -cp ~/.m2/repository/com/h2database/h2/1.4.194/h2-1.4.194.jar org.h2.tools.Server -tcp -web -browser -tcpAllowOthers -tcpPort 9092 -webPort 8082

和連接字符串:

DB_URL=jdbc:h2:tcp://localhost:9092/~/test2

和配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" p:driverClassName="org.h2.Driver"
      p:url="${DB_URL}"
      p:username="${OPENSHIFT_MYSQL_DB_USERNAME}" p:password="${OPENSHIFT_MYSQL_DB_PASSWORD}"/>

<util:properties id="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
    <prop key="hibernate.hbm2ddl.auto">validate</prop>
    <prop key="hibernate.show_sql">false</prop>
</util:properties>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource" p:packagesToScan="com.fridayweekend.lottery.model"
      p:hibernateProperties-ref="hibernateProperties"/>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory"/>

注意:我已經(jīng)嘗試過DDL-AUTO-我在第一次試運行時嘗試創(chuàng)建架構(gòu),然后只驗證它(以防止重新創(chuàng)建),但是沒有幫助。

我可以通過H2 Web Console檢查數(shù)據(jù)庫。我看到創(chuàng)建了Schema(基于我的帶Java注釋的模型),但是沒有數(shù)據(jù)(即使在sleep或調(diào)試斷點期間)。

另一方面,當我手動添加數(shù)據(jù)(從第一個場景點開始)時,我可以調(diào)試第二個線程看到它并正確更新它,然后測試成功。來自第二個線程的數(shù)據(jù)是持久化的,即使在測試套件完成之后,我也可以看到它。

要使來自主線程(@Test注釋)的數(shù)據(jù)對第二個線程可見,我應該做些什么?

PS。我認為這無關緊要,但是”第二個”線程是這樣調(diào)用的:

    <util:properties id="javaMailProperties">
    <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
    <prop key="mail.imap.socketFactory.fallback">false</prop>
    <prop key="mail.store.protocol">${imap.protocol}</prop>
    <prop key="mail.debug">${imap.debug}</prop>
</util:properties>
<mail:inbound-channel-adapter id="imapAdapter"
                              store-uri="${imap.uri}"
                              channel="recieveEmailChannel"
                              should-delete-messages="false"
                              should-mark-messages-as-read="true"
                              auto-startup="true"
                              java-mail-properties="javaMailProperties">
    <int:poller fixed-delay="${imap.poolerSecondsDelay}" time-unit="SECONDS"/>
</mail:inbound-channel-adapter>
<int:channel id="recieveEmailChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger" level="DEBUG"/>
<int:service-activator input-channel="recieveEmailChannel" ref="emailReceiverService" method="receive"/>

因此,它基本上調(diào)用emailReceiverServicebean的receive方法。當然-我確保方法被調(diào)用(通過向收件箱發(fā)送電子郵件)-但是,正如我所說的-我不認為創(chuàng)建第二個線程的方式是相關的。基本上-它是由Spring配置調(diào)用的(Spring配置通過

傳遞給測試方法

@ContextConfiguration(locations = { "classpath:spring/test-lottery-context.xml", "classpath:spring/test-sms-context.xml" })
@Transactional
public class QueueServiceImplIntegrationTest extends AbstractTransactionalTestNGSpringContextTests {

)。

推薦答案

我在那里找到了根本問題:事務

我的測試是擴展AbstractTransactionalTestNGSpringContextTests,因此事務范圍是整個@Test帶注釋的方法。我將測試更改為簡單地擴展AbstractTestNGSpringContextTests-然后事務的范圍縮小到從@Test注釋方法調(diào)用的特定服務方法(我說的是常規(guī)MVC模式)。這解決了我的問題。

干杯!

這篇關于H2訪問數(shù)據(jù)庫以從單獨的線程訪問測試數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:H2 單獨 數(shù)據(jù)庫 測試數(shù)據(jù) 線程 訪問
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定