SQL Server JDBC: unable to create new native thread


We are using Microsoft's sqljdbc driver 1.2 in combination with Hibernate under Windows. Occasionally we are getting an OutOfMemoryError from the JDBC driver although plenty of memory is still available in the JVM heap. Here's a stack trace of the exception we are getting:

java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:597) at com.microsoft.sqlserver.jdbc.TimeoutTimer.start(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) at org.hibernate.loader.Loader.getResultSet(Loader.java:1808) at org.hibernate.loader.Loader.doQuery(Loader.java:697) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.doList(Loader.java:2228) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125) at org.hibernate.loader.Loader.list(Loader.java:2120) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)

Is it possible that sqljdbc leaks native threads?


According to <a href="http://forums.sun.com/thread.jspa?threadID=5320099" rel="nofollow">this thread</a>, the use of setQueryTimeout on the statement makes the executeQuery open a thread for each query. This can lead to a massive threads usage by the driver and cause memory problems. Not sure it's the case here and not sure it's under your control anyway. So actually, you may have to reduce the Thread stack size to avoid this problem.

Try to add -Xss128k to your JVM settings.

See <a href="http://forums.sun.com/thread.jspa?threadID=645335&tstart=20" rel="nofollow">this thread</a> and <a href="http://forums.sun.com/thread.jspa?threadID=647127" rel="nofollow">this one</a> for more informations on Thread stack sizing and hardware limitations.


I was warned against using this driver. I cite:


Far better would be to use jTDS, since Microsoft's own driver is not supported by Hibernate and has some issues which make it a poor choice.


The favored driver for Hibernate on SqlServer is jTDS <a href="http://jtds.sourceforge.net/" rel="nofollow">http://jtds.sourceforge.net/</a>


  • Join 2 tables where two sets of numbers overlap within the joining columns
  • Cannot inject $ionicHistory into my angularjs controller
  • Hive - Out of Memory Exception - Java Heap Space
  • Getting error in ScriptResource.asxd after migrating website from target framework 3.5 to 4.5
  • Java DecimalFormat creates error when enforcing exponent sign
  • WebDriverWait.until no longer available in Selenium-java-3.2 and Selenium-java-3.3 releases
  • Serialize / deserialize with multiple LocalStrategy PassportJs / ExpressJs
  • Sum, Avg, Max, Min, Count of NULL values
  • Android /java Time Format?
  • Checking for a dictionary key which is a range of integers
  • APC - Unable to load dynamic library
  • Bootstrap error iOS 5.0 simulator in XCode 4.2
  • Java out of memory errors on large WSDLs (play framework)
  • SocketChannel: java.io.IOException: An existing connection was forcibly closed by the remote host
  • NodeJs: Cancel function call if it takes too long
  • golang fails to parse json for reflection created object
  • Contains filter on Microsoft Graph query not working
  • TypeScript Mapped Types: Get element type of array
  • How to check when a method is run in another class in Java (method call listener)?
  • scons: How to deal with dynamic targets?
  • Python String Split on pattern without removing delimiter
  • SMT prover yields 'unknown' despite strong proven assertions
  • Getting access to a binary response byte-by-byte in classic asp/JScript
  • inserting duplicate records with SQL
  • WPF: Resize item size to have all items visible
  • Firebase firestore cloud functions showing trigger type 'unknown'
  • Updates to SolrConfig.xml file are not being reflected
  • Unmarshalling unknown type code exception while resuming
  • phpmailer - How to verify a sent email arrived at its destination
  • Java: java.util.ConcurrentModificationException
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • Appium MobileElement swipe returns unknown server error
  • Building Qt project for C++11 standard
  • CSS Linear-gradient formatting issue accross different browsers
  • Master page gives error
  • Nant, Vault & Windows Integrated Authentication
  • Counter field in MS Access, how to generate?
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?