Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A beginner’s question about creating database connections. #4469

Open
gzlboy opened this issue Nov 25, 2024 · 5 comments
Open

A beginner’s question about creating database connections. #4469

gzlboy opened this issue Nov 25, 2024 · 5 comments

Comments

@gzlboy
Copy link

gzlboy commented Nov 25, 2024

我看到bookstore示例代码这样写

type ServiceContext struct {
	c     config.Config
	Model model.BookModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		c:     c,
		Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache),
	}
}

把model放入ServiceContext 里面,如果项目有几十上百个model,都写在这里显得很臃肿,不知道会有性能问题吗?
另外我跟进sqlx.NewMysql方法看,好像使用了数据库连接池,是不是在logic层方法里面直接创建model实例也可以?如:
image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This is a service. It is responsible for one module. There won’t be too many models. If the business is complex, you can split the service model and there won’t be too many models. Of course, you don’t need to write it like this.

@LeeSeika
Copy link

LeeSeika commented Dec 4, 2024

Oppositely, I think the example u show might have a worse performance. Because there are always dao objects to be created and collected by GC every time the method is called.

@gzlboy
Copy link
Author

gzlboy commented Dec 4, 2024

Oppositely, I think the example u show might have a worse performance. Because there are always dao objects to be created and collected by GC every time the method is called.

Thanks for the reply! At first, I also felt that it was wrong to create a database connection object in the request method. Later, I followed up and found that connection pool technology was used, so I was confused. If there are many tables (50+), wouldn't it be bloated to write them all in ServiceContext? Does everyone write like this?

@wojiukankan
Copy link
Contributor

相反,我认为你展示的示例性能可能会更差。因为每次调用该方法时总会有 dao 对象需要创建并由 GC 收集。

谢谢回复!一开始我也觉得在请求方法中创建数据库连接对象是不对的,后来跟进发现用到了连接池技术,于是就很疑惑,如果表很多(50+),全部写在ServiceContext中岂不臃肿?大家都是这么写的吗?

写到serviceContext里是把serviceContext当容器在用。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


On the contrary, I think the example you showed may perform even worse. Because every time this method is called, there will always be a dao object that needs to be created and collected by the GC.

Thank you for your reply! At first, I thought it was wrong to create a database connection object in the request method. Later, I found out that connection pooling technology was used, so I was confused. If there are many tables (50+), wouldn't it be bloated to write them all in the ServiceContext? Is this how everyone writes?

Writing into serviceContext uses serviceContext as a container.

@kevwan kevwan changed the title 新人请教关于创建数据库连接的一点疑问 A beginner’s question about creating database connections. Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants