Index: trunk/pbs_drmaa/session.c
===================================================================
--- trunk/pbs_drmaa/session.c	(revision 65)
+++ trunk/pbs_drmaa/session.c	(revision 85)
@@ -98,5 +98,4 @@
 
 		self->log_file_initial_size = 0;
-		self->pbs_conn = -1;
 		self->pbs_home = NULL;
 
@@ -123,21 +122,6 @@
 		self->super.missing_jobs = FSD_IGNORE_MISSING_JOBS;
 
-		 {
-			int tries_left = self->max_retries_count;
-			int sleep_time = 1;
-			/*ignore SIGPIPE - otheriwse pbs_disconnect cause the program to exit */
-			signal(SIGPIPE, SIG_IGN);
-retry_connect: /* Life... */
-			self->pbs_conn = pbs_connect( self->super.contact );
-			fsd_log_info(( "pbs_connect(%s) =%d", self->super.contact, self->pbs_conn ));
-			if( self->pbs_conn < 0 && tries_left-- )
-			 {
-				sleep(sleep_time++);
-				goto retry_connect;
-			 }
-
-			if( self->pbs_conn < 0 )
-				pbsdrmaa_exc_raise_pbs( "pbs_connect" );
-		 }
+		self->pbs_connection = pbsdrmaa_pbs_conn_new( (fsd_drmaa_session_t *)self, contact );
+		self->connection_max_lifetime =  30; /* 30 seconds */
 
 	 }
@@ -162,6 +146,5 @@
 	pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
 	self->stop_wait_thread( self );
-	if( pbsself->pbs_conn >= 0 )
-		pbs_disconnect( pbsself->pbs_conn );
+	pbsdrmaa_pbs_conn_destroy(pbsself->pbs_connection);
 	fsd_free( pbsself->status_attrl );
 	fsd_free( pbsself->job_exit_status_file_prefix );
@@ -229,4 +212,6 @@
 	fsd_conf_option_t *max_retries_count = NULL;
 	fsd_conf_option_t *user_state_dir = NULL;
+	fsd_conf_option_t *connection_max_lifetime = NULL;
+
 
 	pbs_home = fsd_conf_dict_get(self->configuration, "pbs_home" );
@@ -234,4 +219,5 @@
 	max_retries_count = fsd_conf_dict_get(self->configuration, "max_retries_count" );
 	user_state_dir = fsd_conf_dict_get(self->configuration, "user_state_dir" );
+	connection_max_lifetime = fsd_conf_dict_get(self->configuration, "connection_max_lifetime");
 
 	if( pbs_home && pbs_home->type == FSD_CONF_STRING )
@@ -274,4 +260,10 @@
 	  }
 
+	if ( connection_max_lifetime && connection_max_lifetime->type == FSD_CONF_INTEGER)
+	  {
+		pbsself->connection_max_lifetime = connection_max_lifetime->val.integer;
+		fsd_log_info(("Max connection lifetime: %d", pbsself->connection_max_lifetime));
+	  }
+
 	if ( wait_thread_sleep_time && wait_thread_sleep_time->type == FSD_CONF_INTEGER)
 	  {
@@ -315,11 +307,8 @@
 pbsdrmaa_session_update_all_jobs_status( fsd_drmaa_session_t *self )
 {
-	volatile bool conn_lock = false;
 	volatile bool jobs_lock = false;
 	pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
 	fsd_job_set_t *jobs = self->jobs;
 	struct batch_status *volatile status = NULL;
-	volatile int tries_left = pbsself->max_retries_count;
-	volatile int sleep_time = 1;
 
 	fsd_log_enter((""));
@@ -327,38 +316,11 @@
 	TRY
 	 {
-		conn_lock = fsd_mutex_lock( &self->drm_connection_mutex );
-retry:
+
 /* TODO: query only for user's jobs pbs_selstat + ATTR_u */
 #ifdef PBS_PROFESSIONAL
-		status = pbs_statjob( pbsself->pbs_conn, NULL, NULL, NULL );
+		status = pbsself->pbs_connection->statjob(pbsself->pbs_connection, NULL, NULL);
 #else
-		status = pbs_statjob( pbsself->pbs_conn, NULL, pbsself->status_attrl, NULL );
+		status = pbsself->pbs_connection->statjob(pbsself->pbs_connection, NULL, pbsself->status_attrl);
 #endif
-		fsd_log_info(( "pbs_statjob( fd=%d, job_id=NULL, attribs={...} ) =%p", pbsself->pbs_conn, (void*)status ));
-		if( status == NULL  &&  pbs_errno != 0 )
-		 {
-			if (pbs_errno == PBSE_PROTOCOL || pbs_errno == PBSE_EXPIRED || pbs_errno == PBSOLDE_PROTOCOL || pbs_errno == PBSOLDE_EXPIRED)
-			 {
-				if ( pbsself->pbs_conn >= 0)
-					pbs_disconnect( pbsself->pbs_conn );
-retry_connect:
-				sleep(sleep_time++);
-				pbsself->pbs_conn = pbs_connect( pbsself->super.contact );
-				if( pbsself->pbs_conn < 0)
-				 {
-					if (tries_left--)
-						goto retry_connect;
-					else
-						pbsdrmaa_exc_raise_pbs( "pbs_connect" );
-				 }
-				else
-					goto retry;
-			 }
-			else
-			 {
-				pbsdrmaa_exc_raise_pbs( "pbs_statjob" );
-			 }
-		 }
-		conn_lock = fsd_mutex_unlock( &self->drm_connection_mutex );
 
 		 {
@@ -421,7 +383,5 @@
 	 {
 		if( status != NULL )
-			pbs_statfree( status );
-		if( conn_lock )
-			conn_lock = fsd_mutex_unlock( &self->drm_connection_mutex );
+			 pbsself->pbs_connection->statjob_free(pbsself->pbs_connection, status );
 		if( jobs_lock )
 			jobs_lock = fsd_mutex_unlock( &jobs->mutex );
