Difference between configuring data source in persistence.xml and in spring configuration files

后端 未结 2 1527
青春惊慌失措
青春惊慌失措 2020-12-13 04:19

I\'ve seen (and done) data source configuration in two ways (the code below is just for demo):

1) configuration inside persistence units, like:

<         


        
2条回答
  •  臣服心动
    2020-12-13 04:37

    It makes a huge difference if you're in a JavaEE container.

    More than personal preference, you're much better off if you follow the second approach with a few modifications.

    In the first case, you're creating your own connection pool and do not profit from the existing connection pool in the container. Thus even if you configured your container to, say, a max of 20 simultaneous connections to the database, you can't guarantee this max as this new connection pool is not restrained by your configuration. Also, you don't profit from any monitoring tools your container provides you.

    In the second case, you're also creating your own connection pool, with the same disadvantages as above. However, you can isolate the definition of this spring bean and only use it in test runs.

    Your best bet is to look up the container's connection pool via JNDI. Then you are sure to respect the data source configurations from the container.

    Use this for running tests.

    
    
       
       
       
       
       
       
    .....
    
    

    Use this when deploying to a JavaEE container

    
    
    
    • Remember to set the JEE schema
    • Although Tomcat is not a full JavaEE container, it does manage data sources via JNDI, so this answer still applies.

提交回复
热议问题