[766] | 1 | /* |
---|
| 2 | Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
| 6 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
| 7 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
| 8 | this software, see the FLOSS License Exception |
---|
| 9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
| 12 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
| 13 | of the License. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
| 16 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
| 17 | See the GNU General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU General Public License along with this |
---|
| 20 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
| 21 | Floor, Boston, MA 02110-1301 USA |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | package testsuite.simple; |
---|
| 28 | |
---|
| 29 | import java.sql.CallableStatement; |
---|
| 30 | import java.sql.Connection; |
---|
| 31 | import java.sql.SQLException; |
---|
| 32 | import java.util.Properties; |
---|
| 33 | |
---|
| 34 | import testsuite.BaseTestCase; |
---|
| 35 | |
---|
| 36 | public class ReadOnlyCallableStatementTest extends BaseTestCase { |
---|
| 37 | public ReadOnlyCallableStatementTest(String name) { |
---|
| 38 | super(name); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | public void testReadOnlyWithProcBodyAccess() throws Exception { |
---|
| 42 | if (versionMeetsMinimum(5, 0)) { |
---|
| 43 | Connection replConn = null; |
---|
| 44 | Properties props = getMasterSlaveProps(); |
---|
| 45 | props.setProperty("autoReconnect", "true"); |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | try { |
---|
| 49 | createProcedure("testProc1", "()\n" |
---|
| 50 | + "READS SQL DATA\n" |
---|
| 51 | + "begin\n" |
---|
| 52 | + "SELECT NOW();\n" |
---|
| 53 | + "end\n"); |
---|
| 54 | |
---|
| 55 | createProcedure("`testProc.1`", "()\n" |
---|
| 56 | + "READS SQL DATA\n" |
---|
| 57 | + "begin\n" |
---|
| 58 | + "SELECT NOW();\n" |
---|
| 59 | + "end\n"); |
---|
| 60 | |
---|
| 61 | replConn = getMasterSlaveReplicationConnection(); |
---|
| 62 | replConn.setReadOnly(true); |
---|
| 63 | |
---|
| 64 | CallableStatement cstmt = replConn.prepareCall("CALL testProc1()"); |
---|
| 65 | cstmt.execute(); |
---|
| 66 | cstmt.execute(); |
---|
| 67 | |
---|
| 68 | cstmt = replConn.prepareCall("CALL `" + replConn.getCatalog() + "`.testProc1()"); |
---|
| 69 | cstmt.execute(); |
---|
| 70 | |
---|
| 71 | cstmt = replConn.prepareCall("CALL `" + replConn.getCatalog() + "`.`testProc.1`()"); |
---|
| 72 | cstmt.execute(); |
---|
| 73 | |
---|
| 74 | } finally { |
---|
| 75 | |
---|
| 76 | if (replConn != null) { |
---|
| 77 | replConn.close(); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | public void testNotReadOnlyWithProcBodyAccess() throws Exception { |
---|
| 84 | if (versionMeetsMinimum(5, 0)) { |
---|
| 85 | |
---|
| 86 | Connection replConn = null; |
---|
| 87 | Properties props = getMasterSlaveProps(); |
---|
| 88 | props.setProperty("autoReconnect", "true"); |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | try { |
---|
| 92 | createProcedure("testProc2", "()\n" |
---|
| 93 | + "MODIFIES SQL DATA\n" |
---|
| 94 | + "begin\n" |
---|
| 95 | + "SELECT NOW();\n" |
---|
| 96 | + "end\n"); |
---|
| 97 | |
---|
| 98 | createProcedure("`testProc.2`", "()\n" |
---|
| 99 | + "MODIFIES SQL DATA\n" |
---|
| 100 | + "begin\n" |
---|
| 101 | + "SELECT NOW();\n" |
---|
| 102 | + "end\n"); |
---|
| 103 | |
---|
| 104 | replConn = getMasterSlaveReplicationConnection(); |
---|
| 105 | replConn.setReadOnly(true); |
---|
| 106 | |
---|
| 107 | CallableStatement cstmt = replConn.prepareCall("CALL testProc2()"); |
---|
| 108 | |
---|
| 109 | try{ |
---|
| 110 | cstmt.execute(); |
---|
| 111 | fail("Should not execute because procedure modifies data."); |
---|
| 112 | } catch (SQLException e) { |
---|
| 113 | assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009"); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | cstmt = replConn.prepareCall("CALL `" + replConn.getCatalog() + "`.testProc2()"); |
---|
| 117 | |
---|
| 118 | try{ |
---|
| 119 | cstmt.execute(); |
---|
| 120 | fail("Should not execute because procedure modifies data."); |
---|
| 121 | } catch (SQLException e) { |
---|
| 122 | assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009"); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | cstmt = replConn.prepareCall("CALL `" + replConn.getCatalog() + "`.`testProc.2`()"); |
---|
| 126 | |
---|
| 127 | try{ |
---|
| 128 | cstmt.execute(); |
---|
| 129 | fail("Should not execute because procedure modifies data."); |
---|
| 130 | } catch (SQLException e) { |
---|
| 131 | assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009"); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | } finally { |
---|
| 136 | |
---|
| 137 | if (replConn != null) { |
---|
| 138 | replConn.close(); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | } |
---|