Commit 83c5157756e57a6896739a3a105fdcbe08418687

Authored by Cleverson Sacramento
1 parent 8f6aabb7
Exists in master

Implementação da estratégia de transação JDBC utilizando o Connection

impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/transaction/JDBCTransaction.java 0 → 100644
... ... @@ -0,0 +1,175 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.transaction;
  38 +
  39 +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY;
  40 +
  41 +import java.io.Serializable;
  42 +import java.sql.Connection;
  43 +import java.util.Collection;
  44 +import java.util.Collections;
  45 +import java.util.HashMap;
  46 +import java.util.Map;
  47 +
  48 +import br.gov.frameworkdemoiselle.annotation.Priority;
  49 +import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer;
  50 +import br.gov.frameworkdemoiselle.util.Beans;
  51 +
  52 +/**
  53 + * Represents the strategy destinated to manage JPA transactions.
  54 + *
  55 + * @author SERPRO
  56 + * @see Transaction
  57 + */
  58 +@Priority(EXTENSIONS_L1_PRIORITY)
  59 +public class JDBCTransaction implements Transaction {
  60 +
  61 + private static final long serialVersionUID = 1L;
  62 +
  63 + private ConnectionProducer producer;
  64 +
  65 + private Map<Connection, Status> cache = Collections.synchronizedMap(new HashMap<Connection, Status>());
  66 +
  67 + private ConnectionProducer getProducer() {
  68 + if (producer == null) {
  69 + producer = Beans.getReference(ConnectionProducer.class);
  70 +
  71 + for (Connection connection : producer.getCache().values()) {
  72 + if (!cache.containsKey(connection)) {
  73 + cache.put(connection, new Status());
  74 + }
  75 + }
  76 + }
  77 +
  78 + return producer;
  79 + }
  80 +
  81 + public Collection<Connection> getDelegate() {
  82 + return getProducer().getCache().values();
  83 + }
  84 +
  85 + @Override
  86 + public void begin() {
  87 + Status status;
  88 + for (Connection connection : getDelegate()) {
  89 + status = cache.get(connection);
  90 + status.setActive(true);
  91 + }
  92 + }
  93 +
  94 + @Override
  95 + public void commit() {
  96 + for (Connection connection : getDelegate()) {
  97 + try {
  98 + connection.commit();
  99 + } catch (Exception cause) {
  100 + throw new TransactionException(cause);
  101 + }
  102 + }
  103 + }
  104 +
  105 + @Override
  106 + public void rollback() {
  107 + for (Connection connection : getDelegate()) {
  108 + try {
  109 + connection.rollback();
  110 + } catch (Exception cause) {
  111 + throw new TransactionException(cause);
  112 + }
  113 + }
  114 + }
  115 +
  116 + @Override
  117 + public void setRollbackOnly() {
  118 + Status status;
  119 + for (Connection connection : getDelegate()) {
  120 + status = cache.get(connection);
  121 + status.setRollbackOnly(true);
  122 + }
  123 + }
  124 +
  125 + @Override
  126 + public boolean isActive() {
  127 + Status status;
  128 + boolean result = true;
  129 +
  130 + for (Connection connection : getDelegate()) {
  131 + status = cache.get(connection);
  132 + result = result && status.isActive();
  133 + }
  134 +
  135 + return result;
  136 + }
  137 +
  138 + @Override
  139 + public boolean isMarkedRollback() {
  140 + Status status;
  141 + boolean result = true;
  142 +
  143 + for (Connection connection : getDelegate()) {
  144 + status = cache.get(connection);
  145 + result = result && status.isMarkedRollback();
  146 + }
  147 +
  148 + return result;
  149 + }
  150 +
  151 + private class Status implements Serializable {
  152 +
  153 + private static final long serialVersionUID = 1L;
  154 +
  155 + private boolean active = false;
  156 +
  157 + private boolean markedRollback = false;
  158 +
  159 + public boolean isActive() {
  160 + return active;
  161 + }
  162 +
  163 + public void setActive(boolean active) {
  164 + this.active = active;
  165 + }
  166 +
  167 + public boolean isMarkedRollback() {
  168 + return markedRollback;
  169 + }
  170 +
  171 + public void setRollbackOnly(boolean markedRollback) {
  172 + this.markedRollback = markedRollback;
  173 + }
  174 + }
  175 +}
... ...