Hibernateutil class (used to connect to multiple databases)

Source: Internet
Author: User
Code: My commonly used hibernateutil class, hey
  1. Public final class hibernateutil {
  2. Private Static sessionfactory;
  3. Private hibernateutil (){
  4. }
  5. // Initialization
  6. Static {
  7. Configuration CFG = new configuration ();
  8. Cfg. Configure ();
  9. Sessionfactory = cfg. buildsessionfactory ();
  10. }
  11. Public static sessionfactory getsessionfactory (){
  12. Return sessionfactory;
  13. }
  14. Public static session getsession (){
  15. Return sessionfactory. opensession ();
  16. }
  17. }

The above is the hibernateutil of a single database. The hibernate. cfg. xml file is under the classpath (SRC folder) by default ).

I wrote a file to Parse Multiple hibernate files. cfg. XML file, access the hibernateutil class of multiple databases (this means that your project can use multiple databases, but I am not clear about the transaction management of multiple databases, ):

Code:
  1. Import org. hibernate. query;
  2. Import org. hibernate. Session;
  3. Import org. hibernate. sessionfactory;
  4. Import org. hibernate. cfg. configuration;
  5. Public final class hbnutil {// create a static sessionfactory according to hibernate. cfg. xml
  6. Private Static sessionfactory SF;
  7. Private Static sessionfactory mssf;
  8. Static {
  9. Try {
  10. Sf = new configuration (). Configure ("/hbn-sqlserver.cfg.xml ")
  11. . Buildsessionfactory ();
  12. Mssf = new configuration (). Configure ("/hbn-mysql.cfg.xml ")
  13. . Buildsessionfactory ();
  14. } Catch (exception e ){
  15. System. Err. println ("% error creating sessionfactory % ");
  16. E. printstacktrace ();
  17. }
  18. }
  19. Public static sessionfactory getsessionfactory (){
  20. Return SF;
  21. }
  22. Public static session getsession (){
  23. Session S = NULL;
  24. If (! SF. isclosed ())
  25. S = SF. opensession ();
  26. Return S;
  27. }
  28. Public static session getsessionbydb (string dbname) {// judge based on dbname
  29. Session S = NULL;
  30. If ("MySQL". Equals (dbname. tolowercase ())){
  31. If (! Mssf. isclosed ())
  32. S = mssf. opensession ();
  33. } Else if ("sqlserver". Equals (dbname. tolowercase ())){
  34. If (! SF. isclosed ())
  35. S = SF. opensession ();
  36. } Else {
  37. System. Out. println ("wrong dbname! ");
  38. }
  39. Return S;
  40. }
  41. Public static void closesessionfactory (){
  42. If (! SF. isclosed ()){
  43. SF. Close ();
  44. }
  45. }
  46. Public static void closesessionfactorybydb (string dbname ){
  47. If ("MySQL". Equals (dbname. tolowercase ())){
  48. If (! Mssf. isclosed ()){
  49. Mssf. Close ();
  50. }
  51. } Else if ("sqlserver". Equals (dbname. tolowercase ())){
  52. If (! SF. isclosed ()){
  53. SF. Close ();
  54. }
  55. } Else {
  56. System. Out. println ("wrong dbname! ");
  57. }
  58. }
  59. }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.