Java Code Examples for org.sql2o.Sql2o#beginTransaction()

The following examples show how to use org.sql2o.Sql2o#beginTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Sql2oIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenBatch_thenMultipleInserts() {
    Sql2o sql2o = new Sql2o("jdbc:hsqldb:mem:testDB", "sa", "");
    try(Connection connection = sql2o.beginTransaction()) {
        connection.createQuery("create table PROJECT_15 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))").executeUpdate();
        Query query = connection.createQuery("INSERT INTO PROJECT_15 (NAME, URL) VALUES (:name, :url)");
        for(int i = 0; i < 1000; i++) {
            query.addParameter("name", "tutorials" + i);
            query.addParameter("url", "https://github.com/eugenp/tutorials" + i);
            query.addToBatch();
        }
        query.executeBatch();
        connection.commit();
    }
    try(Connection connection = sql2o.beginTransaction()) {
        assertEquals(1000L, connection.createQuery("SELECT count(*) FROM PROJECT_15").executeScalar());
    }
}
 
Example 2
Source File: XteaService.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Autowired
public XteaService(
	@Qualifier("Runelite SQL2O") Sql2o sql2o,
	CacheService cacheService
)
{
	this.sql2o = sql2o;
	this.cacheService = cacheService;

	try (Connection con = sql2o.beginTransaction())
	{
		con.createQuery(CREATE_SQL)
			.executeUpdate();
	}
}
 
Example 3
Source File: Sql2oPlayground.java    From ddd-wro-warehouse with MIT License 5 votes vote down vote up
@Test
public void sql2oInsert() throws Exception {
    Sql2o sql2o = new Sql2o("jdbc:postgresql://localhost/postgres", "postgres", "");

    ProductStockEvent event = new ProductStockEvent(
            -1, null, "refNi", "NewEntry", "{\"key\":\"value\"}"
    );

    try (org.sql2o.Connection connection = sql2o.beginTransaction()) {
        connection.createQuery(
                "insert into warehouse.ProductStockHistory(refNo, type, content) " +
                        "values (:refNo, :type, cast(:content AS json))")
                .addParameter("refNo", event.getRefNo())
                .addParameter("type", event.getType())
                .addParameter("content", event.getContent())
                .executeUpdate();
        connection.commit();
    }

    List<ProductStockEvent> events = sql2o.open()
            .createQuery("select * from warehouse.ProductStockHistory where refNo = :refNo order by id")
            .addParameter("refNo", event.getRefNo())
            .executeAndFetch(ProductStockEvent.class);

    Assertions.assertThat(events)
            .isNotEmpty()
            .extracting(ProductStockEvent::getRefNo, ProductStockEvent::getType, ProductStockEvent::getContent)
            .contains(tuple(event.getRefNo(), event.getType(), event.getContent()));
}
 
Example 4
Source File: Sql2oIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenTransactionRollback_thenNoDataInserted() {
    Sql2o sql2o = new Sql2o("jdbc:hsqldb:mem:testDB", "sa", "");
    try(Connection connection = sql2o.beginTransaction()) {
        connection.createQuery("create table PROJECT_12 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))").executeUpdate();
        connection.createQuery("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')").executeUpdate();
        connection.createQuery("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')").executeUpdate();
        connection.rollback();
        List<Map<String, Object>> list = connection.createQuery("SELECT * FROM PROJECT_12").executeAndFetchTable().asList();
        assertEquals(0, list.size());
    }
}