Search in sources :

Example 1 with ExternalTransaction

use of org.apache.cayenne.tx.ExternalTransaction in project cayenne by apache.

the class DataContextProcedureQueryIT method testUpdateNoParam.

@Test
public void testUpdateNoParam() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    ProcedureQuery q = new ProcedureQuery(UPDATE_STORED_PROCEDURE_NOPARAM);
    // since stored procedure commits its stuff, we must use an explicit
    // non-committing transaction
    BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        context.performGenericQuery(q);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
    // check that price have doubled
    SelectQuery select = new SelectQuery(Artist.class);
    select.addPrefetch("paintingArray");
    List<?> artists = context.performQuery(select);
    assertEquals(1, artists.size());
    Artist a = (Artist) artists.get(0);
    Painting p = a.getPaintingArray().get(0);
    assertEquals(2000, p.getEstimatedPrice().intValue());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 2 with ExternalTransaction

use of org.apache.cayenne.tx.ExternalTransaction in project cayenne by apache.

the class ProcedureCallIT method runProcedureSelect.

protected <T> ProcedureResult<T> runProcedureSelect(ProcedureCall<T> q) throws Exception {
    // Sybase blows whenever a transaction wraps a SP, so turn off
    // transactions
    // TODO: it is quite the opposite with PostgreSQL. If an SP returns an
    // open refcursor, it actually expects a TX in progress, so while we
    // don't have refcursor unit tests, this is something to keep in mind
    // e.g.
    // http://stackoverflow.com/questions/16921942/porting-apache-cayenne-from-oracle-to-postgresql
    BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        return q.call(context);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
}
Also used : ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction)

Example 3 with ExternalTransaction

use of org.apache.cayenne.tx.ExternalTransaction in project cayenne by apache.

the class MappedQueryIT method runProcedureSelect.

protected QueryResponse runProcedureSelect(AbstractMappedQuery q) throws Exception {
    // Sybase blows whenever a transaction wraps a SP, so turn off
    // transactions
    // TODO: it is quite the opposite with PostgreSQL. If an SP returns an
    // open refcursor, it actually expects a TX in progress, so while we
    // don't have refcursor unit tests, this is something to keep in mind
    // e.g.
    // http://stackoverflow.com/questions/16921942/porting-apache-cayenne-from-oracle-to-postgresql
    BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        return context.performGenericQuery(q);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
}
Also used : ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction)

Example 4 with ExternalTransaction

use of org.apache.cayenne.tx.ExternalTransaction in project cayenne by apache.

the class DataContextPerformQueryAPIIT method testProcedureQueryStringMapBoolean.

@Test
public void testProcedureQueryStringMapBoolean() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    if (!accessStackAdapter.canMakeObjectsOutOfProcedures()) {
        return;
    }
    createTwoArtistsAndTwoPaintingsDataSet();
    // fetch artist
    Map<String, String> parameters = Collections.singletonMap("aName", "artist2");
    List<?> artists;
    // Sybase blows whenever a transaction wraps a SP, so turn of
    // transactions
    Transaction t = new ExternalTransaction(jdbcEventLogger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        artists = context.performQuery("ProcedureQuery", parameters, true);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
    assertNotNull(artists);
    assertEquals(1, artists.size());
    Artist artist = (Artist) artists.get(0);
    assertEquals(11, ((Number) artist.getObjectId().getIdSnapshot().get(Artist.ARTIST_ID_PK_COLUMN)).intValue());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) Transaction(org.apache.cayenne.tx.Transaction) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) Test(org.junit.Test)

Example 5 with ExternalTransaction

use of org.apache.cayenne.tx.ExternalTransaction in project cayenne by apache.

the class DataContextProcedureQueryIT method testUpdate.

@Test
public void testUpdate() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    ProcedureQuery q = new ProcedureQuery(UPDATE_STORED_PROCEDURE);
    q.addParameter("paintingPrice", new Integer(3000));
    // since stored procedure commits its stuff, we must use an explicit
    // non-committing transaction
    BaseTransaction t = new ExternalTransaction(jdbcEventLogger);
    BaseTransaction.bindThreadTransaction(t);
    try {
        context.performGenericQuery(q);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
    // check that price have doubled
    SelectQuery select = new SelectQuery(Artist.class);
    select.addPrefetch("paintingArray");
    List<?> artists = context.performQuery(select);
    assertEquals(1, artists.size());
    Artist a = (Artist) artists.get(0);
    Painting p = a.getPaintingArray().get(0);
    assertEquals(2000, p.getEstimatedPrice().intValue());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

BaseTransaction (org.apache.cayenne.tx.BaseTransaction)6 ExternalTransaction (org.apache.cayenne.tx.ExternalTransaction)6 Artist (org.apache.cayenne.testdo.testmap.Artist)3 Test (org.junit.Test)3 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 Painting (org.apache.cayenne.testdo.testmap.Painting)2 Transaction (org.apache.cayenne.tx.Transaction)1