⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readonlycallablestatementtest.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
字号:
package testsuite.simple;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import testsuite.regression.ConnectionRegressionTest;

public class ReadOnlyCallableStatementTest extends ConnectionRegressionTest {
	public ReadOnlyCallableStatementTest(String name) {
		super(name);

		// TODO Auto-generated constructor stub
	}

	public void testReadOnlyWithProcBodyAccess() throws Exception {
		if (versionMeetsMinimum(5, 0)) {
			Connection replConn = null;
			Properties props = getMasterSlaveProps();
			props.setProperty("autoReconnect", "true");
	
			
			try {
				createProcedure("testProc1", "()\n"
								+ "READS SQL DATA\n"
								+ "begin\n"
								+ "SELECT NOW();\n"
								+ "end\n");

				createProcedure("`testProc.1`", "()\n"
						+ "READS SQL DATA\n"
						+ "begin\n"
						+ "SELECT NOW();\n"
						+ "end\n");
				
				replConn = getMasterSlaveReplicationConnection();
				replConn.setReadOnly(true);
				
				CallableStatement stmt = replConn.prepareCall("CALL testProc1()");
				stmt.execute();
				stmt.execute();
				
				stmt = replConn.prepareCall("CALL test.testProc1()");
				stmt.execute();
				
				stmt = replConn.prepareCall("CALL test.`testProc.1`()");
				stmt.execute();
				
			} finally {
			
				closeMemberJDBCResources();
				
				if (replConn != null) {
					replConn.close();
				}
			}
		}
	}
	
	public void testNotReadOnlyWithProcBodyAccess() throws Exception {
		if (versionMeetsMinimum(5, 0)) {
			
			Connection replConn = null;
			Properties props = getMasterSlaveProps();
			props.setProperty("autoReconnect", "true");
		
			
			try {
				createProcedure("testProc2", "()\n"
								+ "MODIFIES SQL DATA\n"
								+ "begin\n"
								+ "SELECT NOW();\n"
								+ "end\n");

				createProcedure("`testProc.2`", "()\n"
						+ "MODIFIES SQL DATA\n"
						+ "begin\n"
						+ "SELECT NOW();\n"
						+ "end\n");
				
				replConn = getMasterSlaveReplicationConnection();
				replConn.setReadOnly(true);
				
				CallableStatement stmt = replConn.prepareCall("CALL testProc2()");

				try{
					stmt.execute();
					fail("Should not execute because procedure modifies data.");
				} catch (SQLException e) {
					assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009");
				}

				stmt = replConn.prepareCall("CALL test.testProc2()");

				try{
					stmt.execute();
					fail("Should not execute because procedure modifies data.");
				} catch (SQLException e) {
					assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009");
				}

				stmt = replConn.prepareCall("CALL test.`testProc.2`()");

				try{
					stmt.execute();
					fail("Should not execute because procedure modifies data.");
				} catch (SQLException e) {
					assertEquals("Should error for read-only connection.", e.getSQLState(), "S1009");
				}

				
			} finally {
			
				closeMemberJDBCResources();
				
				if (replConn != null) {
					replConn.close();
				}
			}
		}
	}
	


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -