# Is SRConnection Thread Safe?

**URL:** <https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580>\
**Category:** Module Development\
**Created:** [December 13, 2012, 11:00pm UTC](https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580 "2012-12-13T23:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [December 13, 2012, 11:00pm UTC](https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580/1 "2012-12-13T23:00:07Z")

</div>

Can I use a single database connection among multiple threads without any problem? In other words is SRConnection thread safe?

Another question:  
I have this database query:  
Integer systemBytes = (Integer)con.runScalarQuery(“SELECT TransactionID FROM Messages WHERE TimeSent IS NOT NULL ORDER BY TransactionID DESC”);

Will runScalarQuery automatically add “Limit 1” to this query, so that only one row is returned? I don’t want to add the “Limit 1” myself because that’s not cross-database.

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [December 14, 2012, 10:07pm UTC](https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580/2 "2012-12-14T22:07:21Z")

</div>

In effect, as thread-safe as java.sql.Connection… which is to say, not quite defined. It’s going to depend on the implementation of the driver as to how safe it is.

In general, I wouldn’t recommend hanging on to the connection and reusing it. The connection pool works well, so you should feel free to get, use, and close connections as you need them.

As for your other question, the runScalarQuery function calls Statement.setMaxRows(1), but whether or not that’s used for optimization is up to the db driver. SRConnection does, however, have the runPrepLimitQuery, which will apply the limit, or you can do what it does, and use `conn.getParentDatasource().getTranslator().applyLimit("SELECT ...", 1)`  
to build your query.

Regards,

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [December 14, 2012, 10:27pm UTC](https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580/3 "2012-12-14T22:27:26Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [December 17, 2012, 6:13pm UTC](https://forum.inductiveautomation.com/t/is-srconnection-thread-safe/5580/4 "2012-12-17T18:13:42Z")

</div>

Completely incidentally, this morning I happened to learn that the [tt]setMaxRows[/tt] call gets translated by the MySQL Driver into a “SET SQL\_SELECT\_LIMIT=1” call. Couldn’t find too much information about how this compares to “limit” in regards to performance, but I would hope that it would be comparable.

Just reminded me of this thread, so thought I’d post.
