It’s not uncommon to write data to the database and the filesystem in the same transaction. The challenge is to remove files from the filesystem when the transaction fails. It is possible to combine database Spring transactions with files on the filesystem .
The following example has a Spring-managed transactional method which will first write some data to the database. After doing so it will create a file on the filesystem.

Read more »

When running a JUnit test with the SpringJUnit4ClassRunner the Spring context is specified with the ContextConfiguration annotation.

1
2
3
4
5
6
7
8
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-context.xml")
public class MyTests {
@Test
public void aTest() {
}
}

Creating a Spring Context takes some time depending on the size of the application and the classpath that needs to be scanned. Therefore a Context will be stored in the Spring Context cache so that it can be reused for a different test-case.

Sometimes however your tests will change some values in the Spring Context which may cause other tests to fail. The solution is to clean the Spring Context between tests. This can be done with the DirtiesContext annotation.

Read more »

The larger your Servlet application becomes the more time it needs to start. The same thing happens with the time it takes to stop an application.

By default a Tomcat server gives an application 5 seconds to terminate before the “Tomcat did not stop in time. PID file was not removed.” messages is logged. 5 seconds isn’t a lot of time, especially when the application needs to close a connection pool or perform some cleanup tasks.

Read more »

During quite a few projects I’ve ran into issues with the performance of fetching collections in Hibernate. In most cases these performance problems could be fixed by switching from the default fetching strategy to a more suitable alternative. To explain the different ways of fetching collections I’ve created an ‘explained by example’ guide. For those not familiar with the subject it could provide a good place to start tweaking Hibernate performance.

Read more »