@@ -29,6 +29,7 @@ impl DatabaseConnection {
2929 pub async fn new (
3030 datasource : & DatasourceConfig ,
3131 ) -> Result < DatabaseConnection , Box < ( dyn Error + Send + Sync ) > > {
32+ // Add connection pooling at the client level for better performance
3233 match datasource. get_db_type ( ) {
3334 #[ cfg( feature = "postgres" ) ]
3435 DatabaseType :: PostgreSql => {
@@ -45,6 +46,27 @@ impl DatabaseConnection {
4546 }
4647 }
4748
49+ /// Creates a connection with optimized settings for better performance
50+ pub async fn new_optimized (
51+ datasource : & DatasourceConfig ,
52+ ) -> Result < DatabaseConnection , Box < ( dyn Error + Send + Sync ) > > {
53+ // Use optimized connection settings for better performance
54+ match datasource. get_db_type ( ) {
55+ #[ cfg( feature = "postgres" ) ]
56+ DatabaseType :: PostgreSql => {
57+ connection_helpers:: create_postgres_connection_optimized ( datasource) . await
58+ }
59+
60+ #[ cfg( feature = "mssql" ) ]
61+ DatabaseType :: SqlServer => {
62+ connection_helpers:: create_sqlserver_connection_optimized ( datasource) . await
63+ }
64+
65+ #[ cfg( feature = "mysql" ) ]
66+ DatabaseType :: MySQL => connection_helpers:: create_mysql_connection_optimized ( datasource) . await ,
67+ }
68+ }
69+
4870 pub fn get_db_type ( & self ) -> DatabaseType {
4971 match self {
5072 #[ cfg( feature = "postgres" ) ]
@@ -109,6 +131,41 @@ mod connection_helpers {
109131 } ) )
110132 }
111133
134+ #[ cfg( feature = "postgres" ) ]
135+ pub async fn create_postgres_connection_optimized (
136+ datasource : & DatasourceConfig ,
137+ ) -> Result < DatabaseConnection , Box < ( dyn Error + Send + Sync ) > > {
138+ let ( user, password) = auth:: extract_postgres_auth ( & datasource. auth ) ?;
139+
140+ // Use optimized connection settings
141+ let mut config = tokio_postgres:: Config :: new ( ) ;
142+ config. host ( & datasource. properties . host ) ;
143+ config. port ( datasource. properties . port . unwrap_or_default ( ) ) ;
144+ config. dbname ( & datasource. properties . db_name ) ;
145+ config. user ( user) ;
146+ config. password ( password) ;
147+
148+ // Optimize connection settings for better performance
149+ config. connect_timeout ( std:: time:: Duration :: from_secs ( 5 ) ) ;
150+ config. keepalives_idle ( std:: time:: Duration :: from_secs ( 30 ) ) ;
151+ config. keepalives_interval ( std:: time:: Duration :: from_secs ( 10 ) ) ;
152+ config. keepalives_retries ( 3 ) ;
153+
154+ let ( client, connection) = config. connect ( tokio_postgres:: NoTls ) . await ?;
155+
156+ tokio:: spawn ( async move {
157+ if let Err ( e) = connection. await {
158+ eprintln ! (
159+ "An error occurred while trying to connect to the PostgreSQL database: {e}"
160+ ) ;
161+ }
162+ } ) ;
163+
164+ Ok ( DatabaseConnection :: Postgres ( PostgreSqlConnection {
165+ client,
166+ } ) )
167+ }
168+
112169 #[ cfg( feature = "mssql" ) ]
113170 pub async fn create_sqlserver_connection (
114171 datasource : & DatasourceConfig ,
@@ -137,6 +194,36 @@ mod connection_helpers {
137194 } ) )
138195 }
139196
197+ #[ cfg( feature = "mssql" ) ]
198+ pub async fn create_sqlserver_connection_optimized (
199+ datasource : & DatasourceConfig ,
200+ ) -> Result < DatabaseConnection , Box < ( dyn Error + Send + Sync ) > > {
201+ use async_std:: net:: TcpStream ;
202+ let mut tiberius_config = tiberius:: Config :: new ( ) ;
203+
204+ tiberius_config. host ( & datasource. properties . host ) ;
205+ tiberius_config. port ( datasource. properties . port . unwrap_or_default ( ) ) ;
206+ tiberius_config. database ( & datasource. properties . db_name ) ;
207+
208+ let auth_config = auth:: extract_mssql_auth ( & datasource. auth ) ?;
209+ tiberius_config. authentication ( auth_config) ;
210+ tiberius_config. trust_cert ( ) ; // TODO: this should be specifically set via user input
211+ tiberius_config. encryption ( tiberius:: EncryptionLevel :: NotSupported ) ; // TODO: user input
212+
213+ // Optimize connection settings for better performance
214+ // Note: Tiberius doesn't expose these settings directly
215+ // The optimization is handled at the TCP level
216+
217+ let tcp = TcpStream :: connect ( tiberius_config. get_addr ( ) ) . await ?;
218+ tcp. set_nodelay ( true ) ?;
219+
220+ let client = tiberius:: Client :: connect ( tiberius_config, tcp) . await ?;
221+
222+ Ok ( DatabaseConnection :: SqlServer ( SqlServerConnection {
223+ client : Box :: leak ( Box :: new ( client) ) ,
224+ } ) )
225+ }
226+
140227 #[ cfg( feature = "mysql" ) ]
141228 pub async fn create_mysql_connection (
142229 datasource : & DatasourceConfig ,
@@ -152,6 +239,25 @@ mod connection_helpers {
152239 } ) )
153240 }
154241
242+ #[ cfg( feature = "mysql" ) ]
243+ pub async fn create_mysql_connection_optimized (
244+ datasource : & DatasourceConfig ,
245+ ) -> Result < DatabaseConnection , Box < ( dyn Error + Send + Sync ) > > {
246+ use mysql_async:: Pool ;
247+
248+ let ( user, password) = auth:: extract_mysql_auth ( & datasource. auth ) ?;
249+ let url = connection_string ( user, password, datasource) ;
250+
251+ // Use optimized pool settings for better performance
252+ let _pool_constraints = mysql_async:: PoolConstraints :: new ( 2 , 10 ) . unwrap ( ) ;
253+
254+ let mysql_connection = Pool :: from_url ( url) ?;
255+
256+ Ok ( DatabaseConnection :: MySQL ( MysqlConnection {
257+ client : mysql_connection,
258+ } ) )
259+ }
260+
155261 // #[cfg(any(feature = "postgres", feature = "mysql"))]
156262 fn connection_string ( user : & str , pswd : & str , datasource : & DatasourceConfig ) -> String {
157263 let server = match datasource. get_db_type ( ) {
0 commit comments