简体   繁体   中英

Creating Temporary tables in Mysql using java on a read-only server results in error

I have a read-only slave for my production database and I am trying to run a procedure that generates a report. The procedure creates a temporary table, inserts some records, and selects the results from the table. This works fine when I run the procedure through the command line interface or a db UI tool (Navicat), the table is created without any issues, I can insert, and receive the correct report.

The problems begin when I try to call that same procedure using the jdbc in Java. When I run the procedure I get the exception:

Exception in thread "main" java.sql.SQLException: The MySQL server is running with the --read-only option so it cannot execute this statement
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
at com.mysql.jdbc.CallableStatement.executeQuery(CallableStatement.java:956)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)

I am using the same user in the java application and from Navicat, and I need to use a temporary table because the results of this report are used in other reports as well. I have looked online but cannot seem to find any reason why this would work normally but not when run through java.

I fixed the problem. I was using a transaction and the START TRANSACTION call was causing the MySQL error when I called the procedure from Java. Adding a check for @@read_only before starting the transaction fixed the issue. (Still don't know why it was working in Navicat and not Java)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM