Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check status of native ODBC connection in matlab?

Brief introduction of the problem:

the main problem is not in the connection procedure, i could connect to database successfully, and insert some rows in my database(firs code block shows this),but after closing the connection if someone tries to insert a row in the database ,matlab will terminate suddenly without any clear error message, (i expect to have a function to check if the connection is open or close or to get an error message to handle the error but non of these happened and just matlab closed because of a fatal error)

i wrote the following code to connect to MS SQL SERVER database in matlab:

conn=database.ODBCConnection('MS SQL SERVER','','');
insert(conn,'trace',{'obj_id','obj_type_id','time_step','pos_x','pos_y','vel_x','vel_y'},[1,1,1,0,0,0,0]);
close(conn);

and every thing was ok.

then i tried to insert another row (to check what is the error message) then Matlab closed (due to fatal error) without showing any error message.

i tried to use following functions to get status of database connection before inserting new raws:

isconnection(conn);
ping(conn);

but it says

Undefined function 'ping' for input arguments of type 'database.ODBCConnection'.

Undefined function 'isconnection' for input arguments of type 'database.ODBCConnection'

even i tried to use try-catch block but it didn't work and Matlab Closed for fatal error.

so i want to know is there any way to chek status of native ODBC to prevent sudden close of matlab in case of a closed connection??


Update:

>> conn=database.ODBCConnection('MS SQL SERVER','','')

conn = 

ODBCConnection with properties:

  Instance: 'MS SQL SERVER'
  UserName: ''
   Message: []
    Handle: [1x1 database.internal.ODBCConnectHandle]
   TimeOut: 0
AutoCommit: 0
      Type: 'ODBCConnection Object'

 >> close(conn)
 >> conn

 conn = 

 ODBCConnection with properties:

  Instance: 'MS SQL SERVER'
  UserName: ''
   Message: []
    Handle: [1x1 database.internal.ODBCConnectHandle]
   TimeOut: 0
AutoCommit: 0
      Type: 'ODBCConnection Object'

no properties or message changed before and after closing the connection, the problem is that i don't know how to check that if a connection is still open or closed in other parts of a program! in this case if i use an insert command when a connection was closed before, matlab suddenly terminates (and show the message MATLAB(R2013B) has stopped working), so i want to know is there any way to check if a native odbc connection has closed before?


Further update

>> conn=database('MS SQL SERVER','','')

conn =

   Instance: 'MS SQL SERVER'
   UserName: ''
     Driver: []
        URL: []
Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect]
    Message: []
     Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection]
    TimeOut: 0
 AutoCommit: 'on'
       Type: 'Database Object'

>> isconnection(conn)

ans =

 1

>> close(conn)
>> isconnection(conn)

ans =

 0

i mean a function like "isconnection" in the example above for jdbc connection which returns 1 if a connection is open and 0 if the connection closed before.

like image 506
Hadi Avatar asked Jun 03 '15 10:06

Hadi


1 Answers

I request you to check the database connection with toolstrip functionality of Matlab. You can find complete guide from here...

You can perform the testing first so that you can ruled out of any problem with server..

Once it is connected successfully..you can check the code.connection settings and apply it in your code accordingly.

Regards,

like image 91
Sandeep Gandhi Avatar answered Oct 04 '22 02:10

Sandeep Gandhi