java - h2 数据库访问来自不同线程的测试数据
问题描述
我的集成测试场景:
- 在 H2 数据库中创建一行
sleep(50000ms)
(与此同时,由于 spring 配置,另一个线程被调用,该线程应该找到在第 1 点创建的行。并更新该行)- 期望从第 1 点开始的一行。已由第 2 点中提到的线程更新。
此方案同时测试配置和实现。这就是我想要实现的目标。
我在所有测试中都使用 H2 数据库,所以决定在这里也使用它。在调试测试场景时,我发现在sleep
- 期间调用的新线程连接到数据库但没有找到创建的行。我浏览了 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"/>
注意:我已经玩过 ddl-auto - 我尝试在第一次试运行时创建模式,然后只验证它(以防止重新创建),但它没有帮助。
我可以通过H2 Web Console
. 我看到模式已创建(基于我的 Java 注释模型),但没有数据存在(即使sleep
在调试断点期间或期间)。
另一方面 - 当我手动添加数据(从第一个场景点)时 - 我可以调试第二个线程看到它并正确更新它,然后测试成功。来自第二个线程的数据被保留,即使在测试套件完成后我也可以看到它。
我应该怎么做才能使来自主(@Test-annotated)线程的数据对第二个线程可见?
PS。我认为这无关紧要,但是像这样调用“第二个”线程:
<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"/>
所以基本上 - 它调用bean的方法receive
。emailReceiverService
当然-我确保调用了该方法(通过向收件箱发送电子邮件)-但是,正如我所说-我认为创建第二个线程的方式并不相关。基本上 - 它由 Spring 配置调用(spring 配置通过
@ContextConfiguration(locations = { "classpath:spring/test-lottery-context.xml", "classpath:spring/test-sms-context.xml" })
@Transactional
public class QueueServiceImplIntegrationTest extends AbstractTransactionalTestNGSpringContextTests {
)。
解决方案
我在那里找到了根本问题:交易!
我的测试正在扩展,AbstractTransactionalTestNGSpringContextTests
因此事务范围是整个带@Test
注释的方法。我将测试更改为简单地扩展- 然后将事务的范围缩小到从带注释的方法AbstractTestNGSpringContextTests
调用的特定服务方法(我说的是常规 MVC 模式) 。@Test
那解决了我的问题。
干杯!
推荐阅读
- html - 将网格间隙添加到网格的一端
- laravel - 如何将 apiReources 方法与`only`一起使用?
- android - TableRow - 获取列数
- json - 使用 flatpak 发送文件
- javascript - 第二个下拉列表为空
- reactjs - react-recharts LineChart 不呈现
- android - InvalidRequestException 无效的客户端密码 - 条纹支付 sdk
- javascript - 在 Selenium Python 中更改元素的显示不起作用
- php - PHPMailer 发送的邮件变成垃圾邮件
- python - pyinstaller 路径和图标问题