01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.orm.jpa.vendor;
18:
19: import java.sql.Connection;
20: import java.sql.SQLException;
21:
22: import javax.persistence.EntityManager;
23: import javax.persistence.PersistenceException;
24:
25: import org.apache.openjpa.persistence.OpenJPAEntityManager;
26:
27: import org.springframework.jdbc.datasource.ConnectionHandle;
28: import org.springframework.jdbc.datasource.SimpleConnectionHandle;
29: import org.springframework.orm.jpa.DefaultJpaDialect;
30: import org.springframework.transaction.TransactionDefinition;
31: import org.springframework.transaction.TransactionException;
32:
33: /**
34: * {@link org.springframework.orm.jpa.JpaDialect} implementation for
35: * Apache OpenJPA. Developed and tested against OpenJPA 0.9.6.
36: *
37: * @author Costin Leau
38: * @since 2.0
39: */
40: public class OpenJpaDialect extends DefaultJpaDialect {
41:
42: @Override
43: public ConnectionHandle getJdbcConnection(
44: EntityManager entityManager, boolean readOnly)
45: throws PersistenceException, SQLException {
46:
47: Connection connection = (Connection) getOpenJPAEntityManager(
48: entityManager).getConnection();
49: return new SimpleConnectionHandle(connection);
50: }
51:
52: @Override
53: public void releaseJdbcConnection(ConnectionHandle conHandle,
54: EntityManager em) throws PersistenceException, SQLException {
55:
56: if (conHandle != null && conHandle.getConnection() != null) {
57: conHandle.getConnection().close();
58: }
59: }
60:
61: @Override
62: public Object beginTransaction(EntityManager entityManager,
63: TransactionDefinition definition)
64: throws PersistenceException, SQLException,
65: TransactionException {
66:
67: super .beginTransaction(entityManager, definition);
68: if (!definition.isReadOnly()) {
69: // Like with TopLink, make sure to start the logic transaction early so that other
70: // participants using the connection (such as JdbcTemplate) run in a transaction.
71: getOpenJPAEntityManager(entityManager).beginStore();
72: }
73: return null;
74: }
75:
76: /**
77: * Return the OpenJPA-specific interface of <code>EntityManager</code>.
78: * @param em the generic <code>EntityManager</code> instance
79: * @return the OpenJPA-specific interface of <code>EntityManager</code>
80: */
81: protected OpenJPAEntityManager getOpenJPAEntityManager(
82: EntityManager em) {
83: return (OpenJPAEntityManager) em;
84: }
85:
86: }
|