component

Daouda 2019-06-12T22:24:18.000900Z

hey guys, I some help to understand component spirit.

Daouda 2019-06-12T22:26:03.002600Z

Let say i have a component do mange my database connection, and from that connection I retrive a database to perform operation.

seancorfield 2019-06-12T22:26:08.002900Z

What do you understand so far / what do you feel you need more insight on?

Daouda 2019-06-12T22:26:36.003500Z

if i lose the connection, that is mean i lost the databse also?

Daouda 2019-06-12T22:27:16.004400Z

Actually i want o use component to manage my database connexion

Daouda 2019-06-12T22:27:25.004800Z

and think about the best way to do that

Daouda 2019-06-12T22:27:50.005700Z

I am using monger

seancorfield 2019-06-12T22:28:56.006900Z

I'll give an example of how we use Component at work in that area: we have a Configuration Component -- start loads the configuration from a file etc, stop basically does nothing; we have a Database Component -- it depends on Configuration (for database host/user/password etc), start builds a connection pool (a datasource), stop shuts it down.

seancorfield 2019-06-12T22:30:15.008900Z

I don't know how Monger handles reconnecting but I think the underlying Java driver for MongoDB takes care of that, so whatever Monger does to build that "object" should be your start function, and then assoc it into the component.

Daouda 2019-06-12T22:31:06.010200Z

I see people having component for connection and for database. And i really don’t get the point about doing that. since to me the database retrive from a connection is tight that connection.

seancorfield 2019-06-12T22:31:30.010700Z

Then you'll pass the Database Component around in your code (say as a database argument to functions) and then get the MongoDB connection pool from that any time you need to do a DB operation: (:mongodb database) for example.

seancorfield 2019-06-12T22:32:07.011400Z

The key benefit of component is to standardize the start/stop lifecycle aspect of "resource"-like things and encapsulate this "managed state" inside the component.

seancorfield 2019-06-12T22:32:26.011800Z

And to automate the dependency management stuff (Database depends-on Configuration etc).

seancorfield 2019-06-12T22:34:31.013100Z

I would always use a connection pool that will manage reconnections. It'll be a mutable Java object (pretty much by definition). Passing it around inside a Component lets you manage start/stop which is especially useful in the REPL and also in tests.

seancorfield 2019-06-12T22:36:18.015Z

We actually have several connection pools in our Database Component. For our main DB, for our billing DB, for our messaging DB. Back when we used MongoDB as well (via CongoMongo), it used to contain our various MongoDB connection pools as well (again a main :mongo and a :mongo-log connection pool).

seancorfield 2019-06-12T22:36:23.015200Z

Does that help?

Daouda 2019-06-12T22:37:07.015900Z

yeah that helps a lot

seancorfield 2019-06-12T22:37:10.016100Z

I'm not sure what you mean by "the database retrive from a connection is tight that connection"

Daouda 2019-06-12T22:37:12.016200Z

really

Daouda 2019-06-12T22:37:59.017400Z

On monger i get a connection and from that connection a take a database to perfom action on.

seancorfield 2019-06-12T22:38:18.018100Z

Ah, yeah, Mongo has kinda weird terminology.

Daouda 2019-06-12T22:38:34.018500Z

so i think i should only have connection of monger and retrieve from it the database when i need to do something

seancorfield 2019-06-12T22:38:51.019Z

The "connection" is a connection pool manager I think, right?

Daouda 2019-06-12T22:38:57.019300Z

yeah

seancorfield 2019-06-12T22:39:33.020Z

And, yeah, you would keep that in your Component and wherever you do a Monger operation, you get that from your Component and then get the DB handle for the operation.

Daouda 2019-06-12T22:40:23.021Z

that is the way i think it should be

Daouda 2019-06-12T22:40:59.022Z

check this link

seancorfield 2019-06-12T22:41:09.022600Z

Assuming you have an application Component that has the configuration and database Components in it, you'd do stuff like

(monger/some-op (-> application :database :connection (monger/get-db :my-db)) ...)

Daouda 2019-06-12T22:41:33.023100Z

connection is a component and database is another component

seancorfield 2019-06-12T22:42:05.024Z

I wouldn't do that.

Daouda 2019-06-12T22:42:19.024300Z

hanhaaaaannnnnnnn

Daouda 2019-06-12T22:43:27.025400Z

because let say i took a database from a pool and that pool die for some reason. What happen with the database took from it?

seancorfield 2019-06-12T22:44:34.026600Z

Components are really for global things that have a specific start/stop lifecycle -- creating a connection pool, for example -- but getting a DB handle is something short-lived, just for the operation you are performing. It doesn't belong in a Component.

Daouda 2019-06-12T22:45:41.027Z

Your explanation make sense a lot to me

Daouda 2019-06-12T22:46:03.027400Z

thank you very much for taking time to answer me

seancorfield 2019-06-12T22:47:58.027900Z

Happy to help -- we're heavy users of Component at work.

Daouda 2019-06-12T22:48:20.028500Z

I am thinking about having a component for database which will handle the connection pool actually

Daouda 2019-06-12T22:48:39.029Z

and have a protocol to define functions for my database

Daouda 2019-06-12T22:49:45.030Z

I hope i will reach the point thta i will really be confortable with components

seancorfield 2019-06-12T22:50:13.030700Z

Unless your database interactions are fairly small/well-defined, I wouldn't bother with a protocol for them. Unless you really are going to have multiple implementations?

Daouda 2019-06-12T22:50:38.030900Z

no no

Daouda 2019-06-12T22:50:45.031200Z

just one database

Daouda 2019-06-12T22:51:12.031700Z

but the interaction are fairly small

seancorfield 2019-06-12T22:51:35.032600Z

Protocols only really make sense when you have multiple implementations (for different JVM-level types).

Daouda 2019-06-12T22:51:35.032800Z

actually i am build an api just to learn clojure dev good practices

Daouda 2019-06-12T22:51:52.033100Z

i see

seancorfield 2019-06-12T22:53:04.033300Z

This is still good advice about Clojure types https://cemerick.com/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form/

πŸ‘ 1
Daouda 2019-06-12T22:55:52.034100Z

thank you very much for the link πŸ™‚