This is an automated email from the ASF dual-hosted git repository. dbarnes pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/develop by this push: new 0f75926 GEODE-5313: User Guide - consolidate transaction coding examples 0f75926 is described below commit 0f759269a61ddb81bd3fcf7a3f9529127a26944a Author: Dave Barnes <dbar...@pivotal.io> AuthorDate: Mon Jun 11 16:09:08 2018 -0700 GEODE-5313: User Guide - consolidate transaction coding examples --- .../source/subnavs/geode-subnav.erb | 16 +- .../transactions/jca_adapter_example.html.md.erb | 51 ------ .../transaction_coding_examples.html.md.erb | 172 ++++++++++++++++++++- .../transaction_suspend_resume_example.html.md.erb | 38 ----- .../transactional_function_example.html.md.erb | 72 --------- .../transactions/transactions_overview.html.md.erb | 67 -------- 6 files changed, 169 insertions(+), 247 deletions(-) diff --git a/geode-book/master_middleman/source/subnavs/geode-subnav.erb b/geode-book/master_middleman/source/subnavs/geode-subnav.erb index b9d042e..ad09b17 100644 --- a/geode-book/master_middleman/source/subnavs/geode-subnav.erb +++ b/geode-book/master_middleman/source/subnavs/geode-subnav.erb @@ -1493,22 +1493,8 @@ limitations under the License. <li> <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/monitor_troubleshoot_transactions.html">Monitoring and Troubleshooting Transactions</a> </li> - <li class="has_submenu"> + <li> <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/transaction_coding_examples.html">Transaction Coding Examples</a> - <ul> - <li> - <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/transactions_overview.html">Basic Transaction Example</a> - </li> - <li> - <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/transaction_suspend_resume_example.html">Basic Suspend and Resume Transaction Example</a> - </li> - <li> - <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/transactional_function_example.html">Transaction Embedded within a Function Example</a> - </li> - <li> - <a href="/docs/guide/<%=vars.product_version_nodot%>/developing/transactions/jca_adapter_example.html">JCA Resource Adapter Example</a> - </li> - </ul> </li> </ul> </li> diff --git a/geode-docs/developing/transactions/jca_adapter_example.html.md.erb b/geode-docs/developing/transactions/jca_adapter_example.html.md.erb deleted file mode 100644 index 1c7b420..0000000 --- a/geode-docs/developing/transactions/jca_adapter_example.html.md.erb +++ /dev/null @@ -1,51 +0,0 @@ ---- -title: JCA Resource Adapter Example ---- - -<!-- -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. ---> - -This example shows how to use the JCA Resource Adapter in <%=vars.product_name%> . - -``` pre -Hashtable env = new Hashtable(); -env.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”); -env.put(Context.PROVIDER_URL, “t3://localhost:7001”); -Context ctx = new InitialContext(env); -UserTransaction utx = (UserTransaction) ctx.lookup(“javax.transaction.UserTransaction”); -utx.begin(); - // the XA Resource -javax.sql.DataSource ds = (DataSource) ctx.lookup(“derby”); -javax.sql.Connection derbyConn = ds.getConnection(); -Statement stmt = conn.createStatement(); -stmt.executeUpdate(“insert into test values(2,4) “); - // do ConnectionFactory lookup -GFConnectionFactory cf = (GFConnectionFactory) ctx.lookup(“gfe/jca”); - - // Obtaining the connection begins the LocalTransaction. - // If this is absent, operations will not be part of any transaction. -GFConnection conn = cf.getConnection(); - -testRegion.put(“foo”, “bar-”); -utx.commit(); - - // the connection can also be closed within the transaction -derbyConn.close(); -conn.close(); -``` - - diff --git a/geode-docs/developing/transactions/transaction_coding_examples.html.md.erb b/geode-docs/developing/transactions/transaction_coding_examples.html.md.erb index c2dd517..2d1854f 100644 --- a/geode-docs/developing/transactions/transaction_coding_examples.html.md.erb +++ b/geode-docs/developing/transactions/transaction_coding_examples.html.md.erb @@ -21,20 +21,184 @@ limitations under the License. This section provides several code examples for writing and executing transactions. -- **[Basic Transaction Example](transactions_overview.html)** +- **[Basic Transaction Example](#basic_transaction_example)** This example operates on two replicated regions. It begins a transaction, updates one entry in each region, and commits the result. -- **[Basic Suspend and Resume Transaction Example](transaction_suspend_resume_example.html)** +- **[Basic Suspend and Resume Transaction Example](#suspend_resume_example)** This example suspends and resumes a transaction. -- **[Transaction Embedded within a Function Example](transactional_function_example.html)** +- **[Transaction Embedded within a Function Example](#transactional_function_example)** This example demonstrates a function that does transactional updates to Customer and Order regions. -- **[JCA Resource Adapter Example](jca_adapter_example.html)** +- **[JCA Resource Adapter Example](#jca_adapter_example)** This example shows how to use the JCA Resource Adapter in <%=vars.product_name%> . +## <a id="basic_transaction_example" class="no-quick-link"></a>Basic Transaction Example + +This example operates on two replicated regions. It begins a transaction, updates one entry in each region, and commits the result. + +If the commit fails, it will be due to a `CommitConflictException`, which implies that a concurrent access caused a change to one of the items operated on within this transaction. This code fragment catches the exception, and it repeats the transaction attempt until the commit succeeds. + +``` pre +Cache c = new CacheFactory().create(); + +Region<String, Integer> cash = c.createRegionFactory<String, Integer>() + .setDataPolicy(DataPolicy.REPLICATE) + .create("cash"); + +Region<String, Integer> trades = c.createRegionFactory<String, Integer>() + .setDataPolicy(DataPolicy.REPLICATE) + .create("trades"); + +CacheTransactionManager txmgr = c.getCacheTransactionManager(); +boolean commitConflict = false; +do { + try { + txmgr.begin(); + final String customer = "Customer1"; + final Integer purchase = Integer.valueOf(1000); + // Decrement cash + Integer cashBalance = cash.get(customer); + Integer newBalance = + Integer.valueOf((cashBalance != null ? cashBalance : 0) + - purchase); + cash.put(customer, newBalance); + // Increment trades + Integer tradeBalance = trades.get(customer); + newBalance = + Integer.valueOf((tradeBalance != null ? tradeBalance : 0) + + purchase); + + trades.put(customer, newBalance); + txmgr.commit(); + commitConflict = false; + } + catch (CommitConflictException conflict) { + commitConflict = true; + } +} while (commitConflict); +``` + +## <a id="suspend_resume_example" class="no-quick-link"></a>Basic Suspend and Resume Transaction Example + +This example suspends and resumes a transaction. + +``` pre + CacheTransactionManager txMgr = cache.getCacheTransactionManager(); + + txMgr.begin(); + region.put("key1", "value"); + TransactionId txId = txMgr.suspend(); + assert region.containsKey("key1") == false; + // do other operations that should not be + // part of a transaction + txMgr.resume(txId); + region.put("key2", "value"); + txMgr.commit(); +``` + + +## <a id="transactional_function_example" class="no-quick-link"></a>Transaction Embedded within a Function Example + +This example demonstrates a function that does transactional updates to Customer and Order regions. + +``` pre +/** + * This function does transactional updates to customer and order regions + */ +public class TransactionalFunction extends FunctionAdapter { + + private Random random = new Random(); + /* (non-Javadoc) + * @see org.apache.geode.cache.execute.FunctionAdapter#execute(org.apache.geode.cache.execute.FunctionContext) + */ + @Override + public void execute(FunctionContext context) { + RegionFunctionContext rfc = (RegionFunctionContext)context; + Region<CustomerId, String> custRegion = rfc.getDataSet(); + Region<OrderId, String> + orderRegion = custRegion.getRegionService().getRegion("order"); + + CacheTransactionManager + mgr = CacheFactory.getAnyInstance().getCacheTransactionManager(); + CustomerId custToUpdate = (CustomerId)rfc.getFilter().iterator().next(); + OrderId orderToUpdate = (OrderId)rfc.getArguments(); + System.out.println("Starting a transaction..."); + mgr.begin(); + int randomInt = random.nextInt(1000); + System.out.println("for customer region updating "+custToUpdate); + custRegion.put(custToUpdate, + "updatedCustomer_"+custToUpdate.getCustId()+"_"+randomInt); + System.out.println("for order region updating "+orderToUpdate); + orderRegion.put(orderToUpdate, + "newOrder_"+orderToUpdate.getOrderId()+"_"+randomInt); + mgr.commit(); + System.out.println("transaction completed"); + context.getResultSender().lastResult(Boolean.TRUE); + } + + /* (non-Javadoc) + * @see org.apache.geode.cache.execute.FunctionAdapter#getId() + */ + @Override + public String getId() { + return "TxFunction"; + } + +} +``` + +## <a id="jca_adapter_example" class="no-quick-link"></a>JCA Resource Adapter Example + +<!-- +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. +--> + +This example shows how to use the JCA Resource Adapter in <%=vars.product_name%> . + +``` pre +Hashtable env = new Hashtable(); +env.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”); +env.put(Context.PROVIDER_URL, “t3://localhost:7001”); +Context ctx = new InitialContext(env); +UserTransaction utx = (UserTransaction) ctx.lookup(“javax.transaction.UserTransaction”); +utx.begin(); + // the XA Resource +javax.sql.DataSource ds = (DataSource) ctx.lookup(“derby”); +javax.sql.Connection derbyConn = ds.getConnection(); +Statement stmt = conn.createStatement(); +stmt.executeUpdate(“insert into test values(2,4) “); + // do ConnectionFactory lookup +GFConnectionFactory cf = (GFConnectionFactory) ctx.lookup(“gfe/jca”); + + // Obtaining the connection begins the LocalTransaction. + // If this is absent, operations will not be part of any transaction. +GFConnection conn = cf.getConnection(); + +testRegion.put(“foo”, “bar-”); +utx.commit(); + + // the connection can also be closed within the transaction +derbyConn.close(); +conn.close(); +``` + + diff --git a/geode-docs/developing/transactions/transaction_suspend_resume_example.html.md.erb b/geode-docs/developing/transactions/transaction_suspend_resume_example.html.md.erb deleted file mode 100644 index 7944cde..0000000 --- a/geode-docs/developing/transactions/transaction_suspend_resume_example.html.md.erb +++ /dev/null @@ -1,38 +0,0 @@ ---- -title: Basic Suspend and Resume Transaction Example ---- - -<!-- -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. ---> - -This example suspends and resumes a transaction. - -``` pre - CacheTransactionManager txMgr = cache.getCacheTransactionManager(); - - txMgr.begin(); - region.put("key1", "value"); - TransactionId txId = txMgr.suspend(); - assert region.containsKey("key1") == false; - // do other operations that should not be - // part of a transaction - txMgr.resume(txId); - region.put("key2", "value"); - txMgr.commit(); -``` - - diff --git a/geode-docs/developing/transactions/transactional_function_example.html.md.erb b/geode-docs/developing/transactions/transactional_function_example.html.md.erb deleted file mode 100644 index 2b8a8c6..0000000 --- a/geode-docs/developing/transactions/transactional_function_example.html.md.erb +++ /dev/null @@ -1,72 +0,0 @@ ---- -title: Transaction Embedded within a Function Example ---- - -<!-- -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. ---> - -This example demonstrates a function that does transactional updates to Customer and Order regions. - -<a id="concept_22331B3DBFAB4C0BA95EF103BFB71257__section_73662C16E0BF4E4780F737C45DBD3137"></a> - -``` pre -/** - * This function does transactional updates to customer and order regions - */ -public class TransactionalFunction extends FunctionAdapter { - - private Random random = new Random(); - /* (non-Javadoc) - * @see org.apache.geode.cache.execute.FunctionAdapter#execute(org.apache.geode.cache.execute.FunctionContext) - */ - @Override - public void execute(FunctionContext context) { - RegionFunctionContext rfc = (RegionFunctionContext)context; - Region<CustomerId, String> custRegion = rfc.getDataSet(); - Region<OrderId, String> - orderRegion = custRegion.getRegionService().getRegion("order"); - - CacheTransactionManager - mgr = CacheFactory.getAnyInstance().getCacheTransactionManager(); - CustomerId custToUpdate = (CustomerId)rfc.getFilter().iterator().next(); - OrderId orderToUpdate = (OrderId)rfc.getArguments(); - System.out.println("Starting a transaction..."); - mgr.begin(); - int randomInt = random.nextInt(1000); - System.out.println("for customer region updating "+custToUpdate); - custRegion.put(custToUpdate, - "updatedCustomer_"+custToUpdate.getCustId()+"_"+randomInt); - System.out.println("for order region updating "+orderToUpdate); - orderRegion.put(orderToUpdate, - "newOrder_"+orderToUpdate.getOrderId()+"_"+randomInt); - mgr.commit(); - System.out.println("transaction completed"); - context.getResultSender().lastResult(Boolean.TRUE); - } - - /* (non-Javadoc) - * @see org.apache.geode.cache.execute.FunctionAdapter#getId() - */ - @Override - public String getId() { - return "TxFunction"; - } - -} -``` - - diff --git a/geode-docs/developing/transactions/transactions_overview.html.md.erb b/geode-docs/developing/transactions/transactions_overview.html.md.erb deleted file mode 100644 index 3daa989..0000000 --- a/geode-docs/developing/transactions/transactions_overview.html.md.erb +++ /dev/null @@ -1,67 +0,0 @@ ---- -title: Basic Transaction Example ---- - -<!-- -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. ---> - -This example operates on two replicated regions. It begins a transaction, updates one entry in each region, and commits the result. - -<a id="concept_F8D96C21C8444F99B47909CDEB86E60A__section_B6818C348224456387DEC5C9D3B5F250"></a> -If the commit fails, it will be due to a `CommitConflictException`, which implies that a concurrent access caused a change to one of the items operated on within this transaction. This code fragment catches the exception, and it repeats the transaction attempt until the commit succeeds. - -``` pre -Cache c = new CacheFactory().create(); - -Region<String, Integer> cash = c.createRegionFactory<String, Integer>() - .setDataPolicy(DataPolicy.REPLICATE) - .create("cash"); - -Region<String, Integer> trades = c.createRegionFactory<String, Integer>() - .setDataPolicy(DataPolicy.REPLICATE) - .create("trades"); - -CacheTransactionManager txmgr = c.getCacheTransactionManager(); -boolean commitConflict = false; -do { - try { - txmgr.begin(); - final String customer = "Customer1"; - final Integer purchase = Integer.valueOf(1000); - // Decrement cash - Integer cashBalance = cash.get(customer); - Integer newBalance = - Integer.valueOf((cashBalance != null ? cashBalance : 0) - - purchase); - cash.put(customer, newBalance); - // Increment trades - Integer tradeBalance = trades.get(customer); - newBalance = - Integer.valueOf((tradeBalance != null ? tradeBalance : 0) - + purchase); - - trades.put(customer, newBalance); - txmgr.commit(); - commitConflict = false; - } - catch (CommitConflictException conflict) { - commitConflict = true; - } -} while (commitConflict); -``` - - -- To stop receiving notification emails like this one, please contact dbar...@apache.org.