Reputation: 1071
I have a Scala class that accesses a database through JDBC:
class DataAccess {
def select = {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://localhost:3306/db"
val username = "root"
val password = "xxxx"
var connection:Connection = null
try {
// make the connection
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
// create the statement, and run the select query
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT name, descrip FROM table1")
while ( resultSet.next() ) {
val name = resultSet.getString(1)
val descrip = resultSet.getString(2)
println("name, descrip = " + name + ", " + descrip)
}
} catch {
case e => e.printStackTrace
}
connection.close()
}
}
I access this class in my Play application, like so:
def testSql = Action {
val da = new DataAccess
da.select()
Ok("success")
}
The method testSql
may be invoked by several users. Question is: could there be a race condition in the while ( resultSet.next() )
loop (or in any other part of the class)?
Note: I need to use JDBC as the SQL statement will be dynamic.
Upvotes: 0
Views: 137
Reputation: 53819
No there cannot.
Each thread is working with a distinct local instance of ResultSet
so there cannot be concurrent access to the same object.
Upvotes: 2