@@ -26,13 +26,20 @@ impl InnerConnectionPool {
2626 }
2727}
2828
29+ /// A simple fixed-size Postgres connection pool.
30+ ///
31+ /// It can be shared across tasks.
2932// Should be a newtype, but blocked by mozilla/rust#9155
3033#[ deriving( Clone ) ]
3134pub struct PostgresConnectionPool {
3235 priv pool : MutexArc < InnerConnectionPool >
3336}
3437
3538impl PostgresConnectionPool {
39+ /// Attempts to create a new pool with the specified number of connections.
40+ ///
41+ /// Returns an error if the specified number of connections cannot be
42+ /// created.
3643 pub fn try_new ( url : & str , pool_size : uint )
3744 -> Result < PostgresConnectionPool , PostgresConnectError > {
3845 let mut pool = InnerConnectionPool {
@@ -52,15 +59,20 @@ impl PostgresConnectionPool {
5259 } )
5360 }
5461
62+ /// A convenience function wrapping `try_new`.
63+ ///
64+ /// Fails if the pool cannot be created.
5565 pub fn new ( url : & str , pool_size : uint ) -> PostgresConnectionPool {
5666 match PostgresConnectionPool :: try_new ( url, pool_size) {
5767 Ok ( pool) => pool,
5868 Err ( err) => fail ! ( "Unable to initialize pool: %s" , err. to_str( ) )
5969 }
6070 }
6171
62- pub fn try_get_connection ( & self ) -> Result < PooledPostgresConnection ,
63- PostgresConnectError > {
72+ /// Retrieves a connection from the pool.
73+ ///
74+ /// If all connections are in use, blocks until one becomes available.
75+ pub fn get_connection ( & self ) -> PooledPostgresConnection {
6476 let conn = unsafe {
6577 do self . pool . unsafe_access_cond |pool, cvar| {
6678 while pool. pool . is_empty ( ) {
@@ -71,20 +83,17 @@ impl PostgresConnectionPool {
7183 }
7284 } ;
7385
74- Ok ( PooledPostgresConnection {
86+ PooledPostgresConnection {
7587 pool : self . clone ( ) ,
7688 conn : Some ( conn)
77- } )
78- }
79-
80- pub fn get_connection ( & self ) -> PooledPostgresConnection {
81- match self . try_get_connection ( ) {
82- Ok ( conn) => conn,
83- Err ( err) => fail ! ( "Unable to get connection: %s" , err. to_str( ) )
8489 }
8590 }
8691}
8792
93+ /// A Postgres connection pulled from a connection pool.
94+ ///
95+ /// It will be returned to the pool when it falls out of scope, even due to
96+ /// task failure.
8897pub struct PooledPostgresConnection {
8998 priv pool : PostgresConnectionPool ,
9099 // TODO remove the Option wrapper when drop takes self by value
@@ -102,24 +111,29 @@ impl Drop for PooledPostgresConnection {
102111}
103112
104113impl PooledPostgresConnection {
114+ /// Like `PostgresConnection::try_prepare`.
105115 pub fn try_prepare < ' a > ( & ' a self , query : & str )
106116 -> Result < NormalPostgresStatement < ' a > , PostgresDbError > {
107117 self . conn . get_ref ( ) . try_prepare ( query)
108118 }
109119
120+ /// Like `PostgresConnection::prepare`.
110121 pub fn prepare < ' a > ( & ' a self , query : & str ) -> NormalPostgresStatement < ' a > {
111122 self . conn . get_ref ( ) . prepare ( query)
112123 }
113124
125+ /// Like `PostgresConnection::try_update`.
114126 pub fn try_update ( & self , query : & str , params : & [ & ToSql ] )
115127 -> Result < uint , PostgresDbError > {
116128 self . conn . get_ref ( ) . try_update ( query, params)
117129 }
118130
131+ /// Like `PostgresConnection::update`.
119132 pub fn update ( & self , query : & str , params : & [ & ToSql ] ) -> uint {
120133 self . conn . get_ref ( ) . update ( query, params)
121134 }
122135
136+ /// `PostgresConnection::in_transaction`.
123137 pub fn in_transaction < T > ( & self , blk : & fn ( & PostgresTransaction ) -> T ) -> T {
124138 self . conn . get_ref ( ) . in_transaction ( blk)
125139 }
0 commit comments