Grails connections behave differently in integration test

We have our own data source that extends BasicDataSource. We redefined the getConnection method, which does a couple of things inside it. When we start webapp outside of testing, when we call the service from the controller, it will capture a new connection and use this connection until the service is completed. All is well. However, inside the integration test, the connection seems to be captured before the test even calls the controller. Stream lower

Normal start: call controller β†’ call dispatcher call method β†’ ​​connection is grabbed β†’ service method is executed and returns to the controller

Integration test: the connection is captured β†’ the call controller from the test β†’ the controller control method call β†’ the service method is executed and returns to the controller

Needless to say, this gives us problems, since proper connection is very important for our application. Thoughts?

Edit: Serious problems still occur. We have reached a point at which we need to avoid creating integration tests or do a manual connection switch (which defeats half the test point)

DataSource.groovy

dataSource { pooled = true dialect="org.hibernate.dialect.OracleDialect" properties { maxActive = 50 maxIdle = 10 initialSize = 10 minEvictableIdleTimeMillis = 1800000 timeBetweenEvictionRunsMillis = 1800000 maxWait = 10000 testWhileIdle = true numTestsPerEvictionRun = 3 testOnBorrow = true } } hibernate { cache.use_second_level_cache = true cache.use_query_cache = true cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider' } 
+6
source share
1 answer

This is not the final answer, but I believe that this is an explanation of what is happening:

  • It works as a web application . Your service class has a transactionManager that has a sessionFactory that receives the connection! Thus, in this case, if you think that the service is "transactional = true", all methods that you call in your services will have "Session.beginTransaction ()" at the beginning of the method (there is a Grails proxy for this, when you set "transactional = true"), which will call this whole stack until getConnection ().

  • Performing an integration test : since Grails does not commit your changes to the database, it always rolls them back! I believe that when you start your integration test, Grills immediately creates a transaction! so he can rollback after that! (which is absolutely correct!), you can confirm that you have a look at the org.codehaus.groovy class . grails.test.support.GrailsTestInterceptor . The init () method is called before your services in your integration test. So this is why getConnection () is called before everything!

Suggestion : You can try to set your integration test class as "transaction = false" and see if getKonnection () does not receive the call at the beginning! Go to the Transactions section in here to see more! Just do not forget that in your test you will have to roll back your transaction! if your transaction = false.

+2
source

Source: https://habr.com/ru/post/894913/


All Articles