本文介紹了H2訪問(wèn)數(shù)據(jù)庫(kù)以從單獨(dú)的線程訪問(wèn)測(cè)試數(shù)據(jù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我的集成測(cè)試場(chǎng)景:
-
在H2數(shù)據(jù)庫(kù)中創(chuàng)建行
sleep(50000ms)(同時(shí),由于Spring配置,將調(diào)用另一個(gè)線程,此線程應(yīng)找到點(diǎn)1中創(chuàng)建的行,并更新此行)預(yù)期從點(diǎn)%1開始有行。已由點(diǎn)%2中提到的線程更新。
此方案同時(shí)測(cè)試配置和實(shí)現(xiàn)。這就是我想要實(shí)現(xiàn)的目標(biāo)。
我在所有測(cè)試中都使用H2數(shù)據(jù)庫(kù),因此決定在這里也使用它。在調(diào)試測(cè)試場(chǎng)景時(shí),我發(fā)現(xiàn)sleep期間調(diào)用的新線程連接到數(shù)據(jù)庫(kù),但沒有找到創(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)嘗試過(guò)DDL-AUTO-我在第一次試運(yùn)行時(shí)嘗試創(chuàng)建架構(gòu),然后只驗(yàn)證它(以防止重新創(chuàng)建),但是沒有幫助。
我可以通過(guò)H2 Web Console檢查數(shù)據(jù)庫(kù)。我看到創(chuàng)建了Schema(基于我的帶Java注釋的模型),但是沒有數(shù)據(jù)(即使在sleep或調(diào)試斷點(diǎn)期間)。
另一方面,當(dāng)我手動(dòng)添加數(shù)據(jù)(從第一個(gè)場(chǎng)景點(diǎn)開始)時(shí),我可以調(diào)試第二個(gè)線程看到它并正確更新它,然后測(cè)試成功。來(lái)自第二個(gè)線程的數(shù)據(jù)是持久化的,即使在測(cè)試套件完成之后,我也可以看到它。
要使來(lái)自主線程(@Test注釋)的數(shù)據(jù)對(duì)第二個(gè)線程可見,我應(yīng)該做些什么?
PS。我認(rèn)為這無(wú)關(guān)緊要,但是”第二個(gè)”線程是這樣調(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方法。當(dāng)然-我確保方法被調(diào)用(通過(guò)向收件箱發(fā)送電子郵件)-但是,正如我所說(shuō)的-我不認(rèn)為創(chuàng)建第二個(gè)線程的方式是相關(guān)的。基本上-它是由Spring配置調(diào)用的(Spring配置通過(guò)
傳遞給測(cè)試方法
@ContextConfiguration(locations = { "classpath:spring/test-lottery-context.xml", "classpath:spring/test-sms-context.xml" })
@Transactional
public class QueueServiceImplIntegrationTest extends AbstractTransactionalTestNGSpringContextTests {
)。
推薦答案
我在那里找到了根本問(wèn)題:事務(wù)!
我的測(cè)試是擴(kuò)展AbstractTransactionalTestNGSpringContextTests,因此事務(wù)范圍是整個(gè)@Test帶注釋的方法。我將測(cè)試更改為簡(jiǎn)單地?cái)U(kuò)展AbstractTestNGSpringContextTests-然后事務(wù)的范圍縮小到從@Test注釋方法調(diào)用的特定服務(wù)方法(我說(shuō)的是常規(guī)MVC模式)。這解決了我的問(wèn)題。
干杯!
這篇關(guān)于H2訪問(wèn)數(shù)據(jù)庫(kù)以從單獨(dú)的線程訪問(wèn)測(cè)試數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,






