PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/clust/src/test/java/org/jboss/as/test/clustering/single/jdbcstore/TransactionalInfinispanManagedBean.java

#
Java | 174 lines | 135 code | 15 blank | 24 comment | 25 complexity | 3048ccd96cd18a8003d373d6389fa361 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.clustering.single.jdbcstore;
  23. import java.sql.Connection;
  24. import java.sql.ResultSet;
  25. import java.sql.Statement;
  26. import javax.annotation.ManagedBean;
  27. import javax.annotation.PostConstruct;
  28. import javax.annotation.Resource;
  29. import javax.naming.Context;
  30. import javax.naming.InitialContext;
  31. import javax.sql.DataSource;
  32. import javax.transaction.UserTransaction;
  33. import org.infinispan.Cache;
  34. /**
  35. * @author Martin Gencur
  36. */
  37. @ManagedBean("infinispan")
  38. public class TransactionalInfinispanManagedBean {
  39. private static final String CACHE_JNDI_NAME = "java:jboss/infinispan/cache/jdbccontainer/jdbccache";
  40. private static final String DATASOURCE_JNDI_NAME = "java:jboss/datasources/ExampleDS";
  41. @Resource(name = CACHE_JNDI_NAME)
  42. private Cache<Integer, Object> cache;
  43. @Resource
  44. private UserTransaction tx;
  45. @PostConstruct
  46. public void start() {
  47. assert cache != null;
  48. assert tx != null;
  49. }
  50. public void testTxPutCommit() throws Exception {
  51. tx.begin();
  52. cache.put(1, "v1");
  53. cache.put(2, "v2");
  54. assert "v1".equals(cache.get(1));
  55. assert "v2".equals(cache.get(2));
  56. assert rowCount() == 0;
  57. tx.commit();
  58. assert "v1".equals(cache.get(1));
  59. assert "v2".equals(cache.get(2));
  60. assert rowCount() == 2;
  61. }
  62. public void testTxPutRollback() throws Exception {
  63. tx.begin();
  64. cache.put(1, "v1");
  65. cache.put(2, "v2");
  66. assert "v1".equals(cache.get(1));
  67. assert "v2".equals(cache.get(2));
  68. assert rowCount() == 0;
  69. tx.rollback();
  70. assert cache.get(3) == null;
  71. assert cache.get(4) == null;
  72. assert rowCount() == 0; // no change in DB
  73. }
  74. public void testTxRemoveCommit() throws Exception {
  75. initializeCache();
  76. tx.begin();
  77. assert "v1".equals(cache.get(1));
  78. assert "v2".equals(cache.get(2));
  79. assert rowCount() == 2;
  80. cache.remove(1);
  81. assert cache.get(1) == null;
  82. assert "v2".equals(cache.get(2));
  83. assert rowCount() == 2;
  84. tx.commit();
  85. assert cache.get(1) == null;
  86. assert "v2".equals(cache.get(2));
  87. assert rowCount() == 1;
  88. }
  89. public void testTxRemoveRollback() throws Exception {
  90. initializeCache();
  91. tx.begin();
  92. assert "v1".equals(cache.get(1));
  93. assert "v2".equals(cache.get(2));
  94. assert rowCount() == 2;
  95. cache.remove(1);
  96. assert cache.get(1) == null;
  97. assert "v2".equals(cache.get(2));
  98. assert rowCount() == 2;
  99. tx.rollback();
  100. assert "v1".equals(cache.get(1));
  101. assert "v2".equals(cache.get(2));
  102. assert rowCount() == 2; // no change in DB
  103. }
  104. public void testTxAlterCommit() throws Exception {
  105. initializeCache();
  106. tx.begin();
  107. assert "v1".equals(cache.get(1));
  108. assert "v2".equals(cache.get(2));
  109. assert rowCount() == 2;
  110. cache.put(1, "v1_new");
  111. assert "v1_new".equals(cache.get(1));
  112. assert "v2".equals(cache.get(2));
  113. assert rowCount() == 2;
  114. tx.commit();
  115. assert "v1_new".equals(cache.get(1));
  116. assert "v2".equals(cache.get(2));
  117. assert rowCount() == 2;
  118. }
  119. public void testTxAlterRollback() throws Exception {
  120. initializeCache();
  121. tx.begin();
  122. assert "v1".equals(cache.get(1));
  123. assert "v2".equals(cache.get(2));
  124. assert rowCount() == 2;
  125. cache.put(1, "v1_new");
  126. assert "v1_new".equals(cache.get(1));
  127. assert "v2".equals(cache.get(2));
  128. assert rowCount() == 2;
  129. tx.rollback();
  130. assert "v1".equals(cache.get(1));
  131. assert "v2".equals(cache.get(2));
  132. assert rowCount() == 2;
  133. }
  134. private int rowCount() throws Exception {
  135. Context ctx = new InitialContext();
  136. DataSource ds = (DataSource) ctx.lookup(DATASOURCE_JNDI_NAME);
  137. Connection conn = ds.getConnection();
  138. Statement s = conn.createStatement();
  139. ResultSet rs = s.executeQuery("SELECT id FROM stringbased_jdbccache");
  140. int rowCount = 0;
  141. while (rs.next()) {
  142. rowCount++;
  143. }
  144. return rowCount;
  145. }
  146. private void initializeCache() throws Exception {
  147. try {
  148. tx.begin();
  149. cache.clear();
  150. cache.put(1, "v1");
  151. cache.put(2, "v2");
  152. assert cache.keySet().size() == 2;
  153. tx.commit();
  154. } catch (Exception e) {
  155. tx.rollback();
  156. throw new RuntimeException(e);
  157. }
  158. }
  159. }