Skip to content

Commit 0a49c5c

Browse files
committed
editoast: use only one connection for infra clone
1 parent 407ed8a commit 0a49c5c

File tree

1 file changed

+6
-7
lines changed
  • editoast/src/views/infra

1 file changed

+6
-7
lines changed

editoast/src/views/infra/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,17 @@ async fn clone(
271271
db_pool: Data<DbPool>,
272272
new_name: Query<InfraForm>,
273273
) -> Result<Json<i64>> {
274-
let db_pool_ref = db_pool.clone();
275274
let mut futures = Vec::<Pin<Box<dyn Future<Output = _>>>>::new();
276275

277276
let infra = infra.into_inner();
278277
let name = new_name.name.clone();
279-
let cloned_infra = Infra::clone(infra, db_pool_ref.clone(), name).await?;
280-
278+
let cloned_infra = Infra::clone(infra, db_pool.clone(), name).await?;
279+
// When creating a connection for each objet, it will a panic with 'Cannot access shared transaction state' in bb8 database pool
280+
// Just one connection fixes it, but partially* defeats the purpose of joining all the requests at the end
281+
// * AsyncPgConnection supports pipeling within one connection, but it won’t run parallel
282+
let mut conn = db_pool.get().await?;
281283
for object in ObjectType::iter() {
282284
let model_table = object.get_table();
283-
let db_pool_ref = db_pool.clone();
284-
let mut conn = db_pool_ref.get().await?;
285285
let model = sql_query(format!(
286286
"INSERT INTO {model_table}(id, obj_id,data,infra_id) SELECT nextval('{model_table}_id_seq'), obj_id,data,$1 FROM {model_table} WHERE infra_id=$2"
287287
))
@@ -300,8 +300,7 @@ async fn clone(
300300
"INSERT INTO {layer_table}(id, obj_id,geographic,schematic,infra_id, angle_geo, angle_sch) SELECT nextval('{layer_table}_id_seq'), obj_id,geographic,schematic,$1,angle_geo,angle_sch FROM {layer_table} WHERE infra_id=$2"
301301
)
302302
};
303-
let db_pool_ref = db_pool.clone();
304-
let mut conn = db_pool_ref.get().await?;
303+
305304
let layer = sql_query(sql)
306305
.bind::<BigInt, _>(cloned_infra.id.unwrap())
307306
.bind::<BigInt, _>(infra)

0 commit comments

Comments
 (0)