11

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.

Hadi
  • 1,203
  • 1
  • 10
  • 20

2 Answers2

2

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,

Community
  • 1
  • 1
Sandeep Gandhi
  • 59
  • 1
  • 2
  • 9
  • tnx a lot, i saw that documentation before, but the main problem is not in the connection procedure, i could connect to database successfully, and insert some row 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 close 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) – Hadi Jun 12 '15 at 13:36
0

As per the documentation you can check status of an existing database.ODBCConnection or database.ODBCCursor in the Database Toolbox by checking the value of the Message property in the database.ODBCConnection object and the database.ODBCCursor Object.

You may need to set Error handling to store using setdbprefs('ErrorHandling','store'). Use setdbprefs('ErrorHandling','report') to switch it back again.

ping and isconnection only work on database connection object and not on database.ODBCConnection objects.

Appleman1234
  • 15,946
  • 45
  • 67
  • tnx a lot, i updated the question in response to your answer, i think the error occur in matlab or in "native odbc" layer, so there is no message in connection object, – Hadi Jun 06 '15 at 17:46