java - Wrapping a JDBC Connection singleton in a ThreadLocal -


i'm working on small crud application use plain jdbc, connection enum-based singleton, after reading first part of java concurrency in practice liked threadlocalapproach write thread-safe code, question :

when wrapping global jdbc connection in threadlocal considered practice ?

when wrapping global jdbc connection in threadlocal considered practice ?

depends lot on particulars. if there large number of threads each 1 of them going open own connection may prohibitive. going have connections stagnate threads lie dormant.

it better use reentrant connection pool. can reuse connections open not in use limit number of connections minimum need work concurrently. apache's dbcp example , thought of.

to quote docs:

creating new connection each user can time consuming (often requiring multiple seconds of clock time), in order perform database transaction might take milliseconds. opening connection per user can unfeasible in publicly-hosted internet application number of simultaneous users can large. accordingly, developers wish share "pool" of open connections between of application's current users. number of users performing request @ given time small percentage of total number of active users, , during request processing time database connection required.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -