简体   繁体   中英

Can I use JDBC to set Micrososft SQL Server read only?

I'm making a Clojure library that runs Microsoft SQL Server in a Docker image. I'm using image 2019-CU14-ubuntu-20.04.

There's a JDBC macro with-db-transaction that can take a read-only parameter. This works with a PostgreSQL image, but not with Microsoft SQL Server. Is there some way I can set a connection to SQL Server as read-only?

JDBC doesn't have a concept of read-only transactions, but it does support read-only connections (but only as a hint, so it doesn't necessarily require support of the underlying database or driver). Some drivers ignore this hint, while others use it to start read-only transactions.

The Microsoft SQL Server JDBC driver doesn't support it; the implementation of Connection.setReadOnly(boolean) in SQLServerConnection does nothing:

@Override
public void setReadOnly(boolean readOnly) throws SQLServerException {
    if (loggerExternal.isLoggable(Level.FINER))
        loggerExternal.entering(loggingClassName, "setReadOnly", readOnly);
    checkClosed();
    // do nothing per spec
    loggerExternal.exiting(loggingClassName, "setReadOnly");
}

In other words, no it is not possible to set a connection as read-only for SQL Server.

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