Matlab数据库工具箱 - 警告:com.mysql.jdbc.Connection@6e544a45不可序列化

时间:2012-12-12 15:16:17

标签: mysql database jdbc matlab

我通过Matlab数据库工具箱连接到MySQL数据库,以便在2个嵌套for循环中反复运行相同的查询。每次迭代后,我都会收到此警告:

Warning: com.mathworks.toolbox.database.databaseConnect@26960369 is not serializable 
  In Import_Matrices_DOandT_julaugsept_inflow_nomettsed at 476 
Warning: com.mysql.jdbc.Connection@6e544a45 is not serializable 
  In Import_Matrices_DOandT_julaugsept_inflow_nomettsed at 476 
Warning: com.mathworks.toolbox.database.databaseConnect@26960369 not serializable 
  In Import_Matrices_DOandT_julaugsept_inflow_nomettsed at 476 
Warning: com.mysql.jdbc.Connection@6e544a45 is not serializable 
  In Import_Matrices_DOandT_julaugsept_inflow_nomettsed at 476 

我的代码基本上是这样构建的:

%Server
host = 
user = 
password = 
dbName = 

%# JDBC parameters
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';

%# Create the database connection object
conn = database(dbName, user , password, jdbcDriver, jdbcString);
setdbprefs('DataReturnFormat', 'numeric');

%Loop
for SegmentNum=3:41;
    for tl=1:15;
    tic;
    sqlquery=['giant string'];
    results = fetch(conn, sqlquery);
    (some code here that saves the results into a few variables)

    save('inflow.mat');
    end
end

time = toc

close(conn);
clear conn

最终,在一些迭代之后,代码将因此错误而崩溃:

Error using database/fetch (line 37)
  Query execution was interrupted

Error in Import_Matrices_DOandT_julaugsept_inflow_nomettsed (line
466)
results = fetch(conn, sqlquery);

昨晚它在25次迭代后出错了。我总共需要做大约600次迭代,而且我不想每25次检查一次。我听说数据库连接对象可能存在内存问题...有没有办法保留我的代码在运行吗?

3 个答案:

答案 0 :(得分:3)

让我们一步一步。

  

警告:com.mathworks.toolbox.database.databaseConnect@26960369不可序列化

这来自这一行

save('inflow.mat');

您正在尝试保存数据库连接。这不起作用。尝试指定您只想保存的变量,它应该更好。

排除这些值有几个技巧,但老实说,我建议你找到你想要保存的最重要的变量,并保存它们。但如果您愿意,可以将this page的解决方案拼凑起来。

save inflow.mat a b c d e

答案 1 :(得分:1)

尝试将查询包装在try catch块中。每当您发现错误时,请重置与数据库的连接,该数据库应释放该对象。

nQuery = 100;

while(nQuery>0)
   try
      query_the_database();
      nQuery = nQuery - 1;
   catch
      reset_database_connection();
   end
end

答案 2 :(得分:0)

最终的主要原因是数据库连接对象是TCP / IP端口,多个进程无法访问同一端口。这就是数据库连接对象未序列化的原因。端口无法序列化。

解决方法是在for循环中创建连接。