Search in sources :

Example 1 with BaseTransaction

use of org.apache.cayenne.tx.BaseTransaction 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 BaseTransaction

use of org.apache.cayenne.tx.BaseTransaction 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 BaseTransaction

use of org.apache.cayenne.tx.BaseTransaction 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 BaseTransaction

use of org.apache.cayenne.tx.BaseTransaction 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)

Example 5 with BaseTransaction

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

the class DataContextProcedureQueryIT method runProcedureSelect.

protected List<DataRow> runProcedureSelect(ProcedureQuery 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.performQuery(q);
    } finally {
        BaseTransaction.bindThreadTransaction(null);
        t.commit();
    }
}
Also used : ExternalTransaction(org.apache.cayenne.tx.ExternalTransaction) BaseTransaction(org.apache.cayenne.tx.BaseTransaction)

Aggregations

BaseTransaction (org.apache.cayenne.tx.BaseTransaction)6 ExternalTransaction (org.apache.cayenne.tx.ExternalTransaction)5 Test (org.junit.Test)3 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 Painting (org.apache.cayenne.testdo.testmap.Painting)2 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 DataChannel (org.apache.cayenne.DataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 QueryResponse (org.apache.cayenne.QueryResponse)1 DataContext (org.apache.cayenne.access.DataContext)1 Constants (org.apache.cayenne.configuration.Constants)1 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)1 Key (org.apache.cayenne.di.Key)1 Module (org.apache.cayenne.di.Module)1