Commit 51cafbf3e54a16bc912aeeaaf7aae3f1ab317a1e
1 parent
56acf0b5
Exists in
master
Correção da falha ao tentar definir o autocommit false em transações
gerenciadas.
Showing
2 changed files
with
19 additions
and
10 deletions
Show diff stats
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ConnectionProducer.java
| ... | ... | @@ -2,6 +2,7 @@ package br.gov.frameworkdemoiselle.internal.producer; |
| 2 | 2 | |
| 3 | 3 | import java.io.Serializable; |
| 4 | 4 | import java.sql.Connection; |
| 5 | +import java.sql.SQLException; | |
| 5 | 6 | import java.util.Collections; |
| 6 | 7 | import java.util.HashMap; |
| 7 | 8 | import java.util.Map; |
| ... | ... | @@ -55,18 +56,18 @@ public class ConnectionProducer implements Serializable { |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | public Connection getConnection(String name) { |
| 58 | - Connection result = null; | |
| 59 | + Connection connection = null; | |
| 59 | 60 | |
| 60 | 61 | if (cache.containsKey(name)) { |
| 61 | - result = cache.get(name); | |
| 62 | + connection = cache.get(name); | |
| 62 | 63 | |
| 63 | 64 | } else { |
| 64 | 65 | try { |
| 65 | - result = producer.create(name).getConnection(); | |
| 66 | - result.setAutoCommit(false); | |
| 66 | + connection = producer.create(name).getConnection(); | |
| 67 | + disableAutoCommit(connection); | |
| 67 | 68 | |
| 68 | - cache.put(name, result); | |
| 69 | - this.logger.info(bundle.getString("connection-was-created", name)); | |
| 69 | + cache.put(name, connection); | |
| 70 | + logger.info(bundle.getString("connection-was-created", name)); | |
| 70 | 71 | |
| 71 | 72 | } catch (Exception cause) { |
| 72 | 73 | // TODO Colocar uma mensagem amigável |
| ... | ... | @@ -75,15 +76,22 @@ public class ConnectionProducer implements Serializable { |
| 75 | 76 | } |
| 76 | 77 | } |
| 77 | 78 | |
| 78 | - return result; | |
| 79 | + return connection; | |
| 80 | + } | |
| 81 | + | |
| 82 | + private void disableAutoCommit(Connection connection) { | |
| 83 | + try { | |
| 84 | + connection.setAutoCommit(false); | |
| 85 | + | |
| 86 | + } catch (SQLException cause) { | |
| 87 | + logger.debug(bundle.getString("set-autocommit-failed")); | |
| 88 | + } | |
| 79 | 89 | } |
| 80 | 90 | |
| 81 | 91 | private String getName(InjectionPoint ip, JDBCConfig config) { |
| 82 | 92 | String result; |
| 83 | 93 | |
| 84 | 94 | if (ip != null && ip.getAnnotated() != null && ip.getAnnotated().isAnnotationPresent(Name.class)) { |
| 85 | - // TODO Quando o comando Beans.getReference é usado para simular injeção, não existe | |
| 86 | - // anotação @Inject então precisamos testar se #getAnnotated() retorna nulo aqui. | |
| 87 | 95 | result = ip.getAnnotated().getAnnotation(Name.class).value(); |
| 88 | 96 | |
| 89 | 97 | } else { |
| ... | ... | @@ -101,7 +109,7 @@ public class ConnectionProducer implements Serializable { |
| 101 | 109 | String result = config.getDefaultDataDourceName(); |
| 102 | 110 | |
| 103 | 111 | if (result != null) { |
| 104 | - this.logger.debug(bundle.getString("getting-default-datasource-name-from-properties", result)); | |
| 112 | + logger.debug(bundle.getString("getting-default-datasource-name-from-properties", result)); | |
| 105 | 113 | } |
| 106 | 114 | |
| 107 | 115 | return result; | ... | ... |
impl/extension/jdbc/src/main/resources/demoiselle-jdbc-bundle.properties
| ... | ... | @@ -34,6 +34,7 @@ |
| 34 | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 35 | 35 | |
| 36 | 36 | more-than-one-datasource-defined=Existe mais de um banco de dados definido. Utilize @{0} no ponto de inje\u00E7\u00E3o ou defina o atributo "frameworkdemoiselle.persistence.default.datasource.name" no arquivo demoiselle.properties. |
| 37 | +set-autocommit-failed=Falha ao tentar executar connection.setAutoCommit(false) numa transa\u00E7\u00E3o gerenciada. N\u00E3o se preocupe, este comportamento \u00E9 esperado. | |
| 37 | 38 | load-jndi-datasource-failed=Falha ao tentar obter a conex\u00E3o "{0}" via JNDI. |
| 38 | 39 | load-duplicated-configuration-failed=Falha no carregamento das configura\u00E7\u00F5es JDBC. Verifique se existem valores duplicados indevidamente no demoiselle.properties. |
| 39 | 40 | getting-default-datasource-name-from-properties=Obtendo a configura\u00E7\u00E3o de banco de dados padr\u00E3o "{0}" a partir do arquivo de configura\u00E7\u00E3o demoiselle.properties. | ... | ... |