Created
January 6, 2016 12:27
-
-
Save robshep/e1367bea2f74e696688b to your computer and use it in GitHub Desktop.
Single file REST controller with transactional service support, in Groovy using Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grapes( | |
@Grab(group='com.h2database', module='h2', version='1.4.190') | |
) | |
import org.springframework.transaction.annotation.Transactional | |
import javax.sql.DataSource; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | |
import org.springframework.transaction.support.TransactionSynchronizationManager | |
@EnableTransactionManagement | |
@RestController | |
@groovy.util.logging.Slf4j | |
class WebApplication | |
{ | |
@Autowired TxService txService; | |
@RequestMapping("/") | |
String home() { | |
txService.call(); | |
return "Hello World\n" | |
} | |
@Configuration | |
public static class Conf | |
{ | |
@Bean | |
public DataSource dataSource() { | |
// no need shutdown, EmbeddedDatabaseFactoryBean will take care of this | |
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); | |
EmbeddedDatabase db = builder | |
.setType(EmbeddedDatabaseType.H2) //.H2 or .DERBY | |
.build(); | |
return db; | |
} | |
} | |
@Service | |
@Transactional | |
public static class TxService | |
{ | |
public void call() | |
{ | |
log.info "{}", org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment