Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java h2数据库访问来自不同线程的测试数据_Java_Spring_Testing_Integration Testing_H2 - Fatal编程技术网

Java h2数据库访问来自不同线程的测试数据

Java h2数据库访问来自不同线程的测试数据,java,spring,testing,integration-testing,h2,Java,Spring,Testing,Integration Testing,H2,我的集成测试场景: 在H2数据库中创建一行 sleep(50000ms)(同时,由于spring配置,调用了另一个线程,该线程应该找到在第1点中创建的行,并更新该行) 从第1点开始期待一行。已由第2点中提到的线程更新 此场景同时测试配置和实现。这就是我想要实现的 我在所有测试中都使用H2数据库,所以决定在这里也使用它。调试测试场景时,我发现在睡眠期间调用的新线程连接到数据库,但没有找到创建的行。我查阅了H2文档,并开始使用: java -cp ~/.m2/repository/com/h2dat

我的集成测试场景:

  • 在H2数据库中创建一行
  • sleep(50000ms)
    (同时,由于spring配置,调用了另一个线程,该线程应该找到在第1点中创建的行,并更新该行)
  • 从第1点开始期待一行。已由第2点中提到的线程更新
  • 此场景同时测试配置和实现。这就是我想要实现的

    我在所有测试中都使用H2数据库,所以决定在这里也使用它。调试测试场景时,我发现在
    睡眠期间调用的新线程连接到数据库,但没有找到创建的行。我查阅了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"/>
    
    所以basicali-它调用
    EmailReceiveService
    bean的
    receive
    方法。当然-我确保了方法被调用(通过向收件箱发送电子邮件)-但是,正如我所说的-我不认为创建第二个线程的方式是相关的。基本上说,它是由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
    ——然后事务的范围缩小到从
    @test
    注释方法调用的特定服务方法(我指的是常规MVC模式)。这就解决了我的问题


    干杯

    我在那里找到了根本问题:交易

    我的测试扩展了
    AbstractTransactionalTestNGSpringContextTests
    ,因此事务范围是完整的
    @test
    带注释的方法。我将我的测试改为简单地扩展
    AbstractTestNGSpringContextTests
    ——然后事务的范围缩小到从
    @test
    注释方法调用的特定服务方法(我指的是常规MVC模式)。这就解决了我的问题

    干杯

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