Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OLINGO-580 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,9 @@ public interface ODataJPAContext {
* @return an instance of type {@link org.apache.olingo.odata2.jpa.processor.api.access.JPAPaging}
*/
public JPAPaging getPaging();

/**
* The transaction context
*/
public ODataJPATransaction getODataJpaTransactionContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public abstract class ODataJPAServiceFactory extends ODataServiceFactory {
private ODataContext oDataContext;
private boolean setDetailErrors = false;
private OnJPAWriteContent onJPAWriteContent = null;
private ODataJPATransaction oDataJPATransaction = null;

/**
* Creates an OData Service based on the values set in
Expand Down Expand Up @@ -201,7 +202,34 @@ public <T extends ODataCallback> T getCallback(final Class<? extends ODataCallba
return (T) onJPAWriteContent;
}
}
return null;

if (oDataJPATransaction != null) {
if (callbackInterface.isAssignableFrom(ODataJPATransaction.class)) {
return (T) oDataJPATransaction;
}
}


return null;
}


/**
* The methods sets the context with a callback implementation for JPA transaction specific content.
* For details refer to {@link ODataJPATransaction}
* @param oDataJPATransaction is an instance of type
* {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction}
*/
protected void setODataJPATransaction(final ODataJPATransaction oDataJPATransaction) {
this.oDataJPATransaction = oDataJPATransaction;
}

/**
* Simple method to retrieve the current ODataJPATransactionContext optimized for fast access
*
* @return the current ODataJPATransactionContext
*/
public ODataJPATransaction getoDataJPATransaction() {
return oDataJPATransaction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
******************************************************************************/
package org.apache.olingo.odata2.jpa.processor.api;

import org.apache.olingo.odata2.api.ODataCallback;

/**
* Interface for JPA-Transaction abstraction. Default implementation is Resource local, while additional
* an override may used to insert JTA compatible transactions as well.
*
*/
public interface ODataJPATransaction extends ODataCallback {

/**
* implement the start of the transaction
*/
public void begin();

/**
* implement the commit of the transaction
*/
public void commit();

/**
* implement the rollback of the transaction
*/
public void rollback();

/**
* implement status of the transaction context
*/
public boolean isActive();


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.api.processor.ODataProcessor;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAPaging;
import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmExtension;

Expand Down Expand Up @@ -166,4 +167,9 @@ public void setPaging(final JPAPaging paging) {
public JPAPaging getPaging() {
return jpaPaging;
}

@Override
public ODataJPATransaction getODataJpaTransactionContext() {
return odataContext.getServiceFactory().getCallback(ODataJPATransaction.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,22 @@ public BatchResponsePart executeChangeSet(final BatchHandler handler, final List
throws ODataException {
List<ODataResponse> responses = new ArrayList<ODataResponse>();
try {
oDataJPAContext.getEntityManager().getTransaction().begin();
oDataJPAContext.getODataJpaTransactionContext().begin();

for (ODataRequest request : requests) {
oDataJPAContext.setODataContext(getContext());
ODataResponse response = handler.handleRequest(request);
if (response.getStatus().getStatusCode() >= HttpStatusCodes.BAD_REQUEST.getStatusCode()) {
// Rollback
oDataJPAContext.getEntityManager().getTransaction().rollback();
oDataJPAContext.getODataJpaTransactionContext().rollback();
List<ODataResponse> errorResponses = new ArrayList<ODataResponse>(1);
errorResponses.add(response);
return BatchResponsePart.responses(errorResponses).changeSet(false).build();
}
responses.add(response);
}
oDataJPAContext.getEntityManager().getTransaction().commit();
oDataJPAContext.getODataJpaTransactionContext().commit();


return BatchResponsePart.responses(responses).changeSet(true).build();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.apache.olingo.odata2.api.edm.EdmEntitySet;
import org.apache.olingo.odata2.api.edm.EdmException;
Expand All @@ -44,6 +43,7 @@
import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAProcessor;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPAModelException;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
Expand Down Expand Up @@ -149,18 +149,17 @@ public void delete(final DeleteUriInfo uriInfo) throws ODataJPARuntimeException

public void save() {
EntityManager em = context.getEntityManager();
EntityTransaction tx = em.getTransaction();

ODataJPATransaction tx = context.getODataJpaTransactionContext();
if (!tx.isActive()) {
em.getTransaction().begin();
tx.begin();
if (sourceJPAEntity != null) {
em.persist(sourceJPAEntity);
}
if (targetJPAEntity != null) {
em.persist(targetJPAEntity);
em.flush();
}
em.getTransaction().commit();
tx.commit();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.apache.olingo.odata2.api.commons.InlineCount;
Expand All @@ -50,6 +49,7 @@
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPATombstoneContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPATombstoneEntityListener;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAFunction;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAMethodContext;
import org.apache.olingo.odata2.jpa.processor.api.access.JPAProcessor;
Expand Down Expand Up @@ -355,7 +355,7 @@ public Object process(DeleteUriInfo uriParserResultView, final String contentTyp
em.remove(selectedObject);
em.flush();
if (isLocalTransaction) {
em.getTransaction().commit();
oDataJPAContext.getODataJpaTransactionContext().commit();
}

} catch (Exception e) {
Expand Down Expand Up @@ -456,7 +456,7 @@ private Object processCreate(final PostUriInfo createView, final InputStream con
em.persist(jpaEntity);
if (em.contains(jpaEntity)) {
if (isLocalTransaction) {
em.getTransaction().commit();
oDataJPAContext.getODataJpaTransactionContext().commit();
}
return jpaEntity;
}
Expand Down Expand Up @@ -507,7 +507,7 @@ private <T> Object processUpdate(PutMergePatchUriInfo updateView,
}
em.flush();
if (isLocalTransaction) {
em.getTransaction().commit();
oDataJPAContext.getODataJpaTransactionContext().commit();
}
} catch (Exception e) {
throw ODataJPARuntimeException.throwException(
Expand Down Expand Up @@ -572,9 +572,9 @@ private List<Object> handlePaging(final Query query, final GetEntitySetUriInfo u
}

private boolean setTransaction() {
final EntityTransaction transaction = em.getTransaction();
if (!transaction.isActive()) {
em.getTransaction().begin();
ODataJPATransaction transactionContext = oDataJPAContext.getODataJpaTransactionContext();
if (!transactionContext.isActive()) {
transactionContext.begin();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
******************************************************************************/
package org.apache.olingo.odata2.jpa.processor.ref.extension;


import org.apache.olingo.odata2.jpa.processor.api.ODataJPATransaction;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

public class ODataJPATransactionLocalDefault implements ODataJPATransaction {

EntityTransaction tx = null;

public ODataJPATransactionLocalDefault(EntityManager em) {
this.tx = em.getTransaction();
}

@Override
public void begin() {
tx.begin();
}

@Override
public void commit() {
tx.commit();
}

@Override
public void rollback() {
tx.rollback();
}

@Override
public boolean isActive() {
return tx.isActive();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
import org.apache.olingo.odata2.jpa.processor.api.model.JPAEdmExtension;
import org.apache.olingo.odata2.jpa.processor.ref.extension.ODataJPATransactionLocalDefault;
import org.apache.olingo.odata2.jpa.processor.ref.extension.OnDBWriteContent;
import org.apache.olingo.odata2.jpa.processor.ref.extension.SalesOrderProcessingExtension;
import org.apache.olingo.odata2.jpa.processor.ref.factory.JPAEntityManagerFactory;
Expand All @@ -50,6 +51,11 @@ public ODataJPAContext initializeODataJPAContext()
oDataJPAContext.setDefaultNaming(false);
setErrorLevel();
setOnWriteJPAContent(onDBWriteContent);
if(getoDataJPATransaction() == null) {
setODataJPATransaction(
new ODataJPATransactionLocalDefault(getODataJPAContext().getEntityManager()));
}


return oDataJPAContext;
}
Expand Down