I'm unsuccessful at getting Spring Data to send postgres a FOR UPDATE with NOWAIT.
I've tried this in the Repository:
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
MyObject findByUuid(String uuid);
Configuration (snippet)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "docsEntityManagerFactory",
transactionManagerRef = "docsTransactionManager",
@Bean(name = "docsEntityManagerFactory")
public EntityManagerFactory docsEntityManagerFactory(DataSource docsDataSource) {
props.setProperty("javax.persistence.lock.timeout", "0");
...
I even inject the EntityManager in my service and this returns 0:
logger.info(em.getProperties().get("javax.persistence.lock.timeout"));
but the above only gets me the "FOR UPDATE", the NOWAIT part isn't being set. Am I setting the right thing (from another post it looks like this should work).
This gets me closer, although I'd prefer not to use raw sql
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
@Query(value = "SELECT * FROM the_table WHERE uuid = :uuid FOR UPDATE NOWAIT", nativeQuery = true)
MyObject findByUuid(@Param("uuid") String uuid);
This does give me a Lock exception, but then the Spring decorated service (proxy) throws a Transaction/Rollback exception on the return, which means I can't give a return value on the method, that I need.
Questions: 1) How do I get Spring Data to add the NOWAIT? 2) Is there a way to return the value? I suspect I need to handle the transaction myself, or change the logic flow? Not sure if Spring handles the NOWAIT if it will behave differently.
I realize there is similar questions, but this is slightly different. I'm using hibernate 4.2.15, spring-data-jpa 1.7.1, postgres sql 9.3.