# Sharing Resources (e.g. DB connection) for Activities in Java

**URL:** <https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966>\
**Category:** Community Support\
**Created:** [November 12, 2020, 7:37am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966 "2020-11-12T07:37:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![cg1972](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cg1972/32/465_2.png) [@cg1972](https://community.temporal.io/u/cg1972)\
**Post date:** [November 12, 2020, 7:37am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/1 "2020-11-12T07:37:42Z")

</div>

Hi, I need to create Activities that access external resources such as cassandra and elastic. To avoid creating new connections each time an Activity is called I wanted to understand if it is possible to share the connections across all Activities. Would this be achieved by passing the connections through when the Activities are created and registered with the worker?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 12, 2020, 5:41pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/2 "2020-11-12T17:41:51Z")

</div>

The activity implementation object is constructed by your code. So you can inject any dependencies into it before registering with a worker.

---

<div class="post-metadata">

**Author:** ![cg1972](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cg1972/32/465_2.png) [@cg1972](https://community.temporal.io/u/cg1972)\
**Post date:** [November 12, 2020, 9:32pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/3 "2020-11-12T21:32:42Z")

</div>

Thanks Maxim.

So if I had something similar to this:

> // create a connection to a database to be used by the Activity  
> DatabaseClient dbClient = new DatabaseClient(connectionParam1, connectionParam2,…);  
> worker.registerActivitiesImplementations(new MyTestActivityWithDBConnectionImpl(dbClient));

If the worker node goes down for some reason Temporal is able to re-create the DatabaseClient used by the Activity? Does the DatabaseClient need to be structured in a particular way in order for Temporal to be able to re-create it? Thanks.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 12, 2020, 11:16pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/4 "2020-11-12T23:16:29Z")

</div>

Temporal recovers state of workflow executions only. The activity implementation object will have to be recreated by your code on the process startup.

---

<div class="post-metadata">

**Author:** ![cg1972](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cg1972/32/465_2.png) [@cg1972](https://community.temporal.io/u/cg1972)\
**Post date:** [November 12, 2020, 11:59pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/5 "2020-11-12T23:59:17Z")

</div>

Could you clarify what you mean by “process startup”?

E.g. if I create a simple workflow that is setup to run every hour as a cron and has a single Activity that checks the status of a database I would:

1. Create the worker
2. Register the workflow implementation with the worker
3. Initialise the Activity and register it with the worker

The workflow starts running every hour and then at some point temporal is restarted. Do steps 1- 3 need to be re-run again at this point for the workflows to continue running?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 13, 2020, 12:09am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/6 "2020-11-13T00:09:07Z")

</div>

The above 3 steps are preparing worker to execute activities and workflows. But you still have to start a workflow execution explicitly, possibly from a different process. The steps 1-3 should be reexecuted on the process restart that is not related to the workflow execution in any way.

---

<div class="post-metadata">

**Author:** ![cg1972](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cg1972/32/465_2.png) [@cg1972](https://community.temporal.io/u/cg1972)\
**Post date:** [November 13, 2020, 12:32am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/7 "2020-11-13T00:32:21Z")

</div>

Thanks Maxim - are you referring to Temporal when you talk about the process restart?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 13, 2020, 1:13am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/8 "2020-11-13T01:13:16Z")

</div>

No, I refer to your worker processes.

---

<div class="post-metadata">

**Author:** ![cg1972](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cg1972/32/465_2.png) [@cg1972](https://community.temporal.io/u/cg1972)\
**Post date:** [November 13, 2020, 5:21am UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/9 "2020-11-13T05:21:24Z")

</div>

Thanks Maxim - my initial thoughts on how the workers ran was incorrect. This makes sense now.

---

<div class="post-metadata">

**Author:** ![Cameron\_Ward](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cameron_ward/32/2494_2.png) [@Cameron\_Ward](https://community.temporal.io/u/Cameron_Ward)\
**Post date:** [November 19, 2023, 6:13pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/10 "2023-11-19T18:13:10Z")

</div>

Did you end up using this solution?  
I can’t work out if it’s best to create the database connection when the worker is started and pass it as a constructor to the Activity like you show or to create the connection each time in the activity.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 19, 2023, 6:20pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/11 "2023-11-19T18:20:06Z")

</div>

It is best to create connections once per process and pass them to the activity constructor. Why do you want to create them per request? It is usually extremely expensive.

---

<div class="post-metadata">

**Author:** ![Cameron\_Ward](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cameron_ward/32/2494_2.png) [@Cameron\_Ward](https://community.temporal.io/u/Cameron_Ward)\
**Post date:** [November 19, 2023, 6:32pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/12 "2023-11-19T18:32:24Z")

</div>

Thanks, I don’t want to create a connection once per process.  
Should I create them in the worker process and pass them to the activity constructor?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 19, 2023, 6:46pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/13 "2023-11-19T18:46:38Z")

</div>

> I don’t want to create a connection once per process.

I’m confused. Do you want to create a connection pe request?

---

<div class="post-metadata">

**Author:** ![Cameron\_Ward](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/cameron_ward/32/2494_2.png) [@Cameron\_Ward](https://community.temporal.io/u/Cameron_Ward)\
**Post date:** [November 19, 2023, 9:22pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/14 "2023-11-19T21:22:45Z")

</div>

Sorry, I meant I do not want to create a connection once per request and instead, only once for the worker.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 20, 2023, 8:55pm UTC](https://community.temporal.io/t/sharing-resources-e-g-db-connection-for-activities-in-java/966/15 "2023-11-20T20:55:07Z")

</div>

> [@Cameron\_Ward](#):
>
> create them in the worker process and pass them to the activity constructor?

Then, create them in the worker process and pass them to the activity constructor.
